Common Exception Types
The else block is optional. It does not run after an exception is handled; it runs only when every statement in the try block completes without raising an exception.
Try it: try / except / else / finally
How an exception jumps out of a try block to the first matching except clause, when else runs, and why finally always runs.
How it works
- Statements in try run until one raises an exception; the rest of try is skipped.
- Python checks the except clauses in order and runs the first one whose type matches.
- If nothing was raised, the else block runs instead.
- finally runs in every case.
Default run (9 steps): Call safe_divide('4'). … Return 25.0. Output was: Worked: 25.0 / Done.
Simplified: One fixed function; int() and / are modelled exactly (including how Python prints floats), but nothing is executed as Python.
Loading the simulation…
Why Exception Handling Has Separate Paths
A try..except statement separates two kinds of work: code that is expected to run normally and code that handles errors. The try block contains the normal statements. The except blocks provide responses for specified exception types. This separation lets Python follow a different execution path when a statement in the try block raises an exception.
The Try and Except Structure
A try..except statement begins with the try keyword, followed by a colon and an indented block of statements. Below that block, one or more except blocks specify exception types to catch. An optional else block can follow the except blocks and runs only when no exception occurs in the try block.
The else block is optional. It does not run after an exception is handled; it runs only when every statement in the try block completes without raising an exception.
Tracing an Exception Through the Try Block
Python begins by executing the statements inside the try block in order. If a statement raises an exception, Python immediately stops executing the remaining statements in that try block. It then checks the except blocks in the order they appear. The first except block whose exception type matches the raised exception runs. The other except blocks are skipped, the else block is skipped, and execution continues after the entire try..except statement.
What do you think happens?
Suppose the first statement in a try block raises an exception. What happens to the later statements in that same try block?
Reveal answer
They are skipped, and Python checks the except blocks.
Python immediately stops executing the rest of the try block when a statement raises an exception. It then checks the except blocks in order.
Choosing the First Matching Handler
Input Cancellation and End of Input
Trace the source example in which an input operation may raise EOFError or KeyboardInterrupt.
- 1Normal input
The input operation completes without either of the two listed exceptions. The except blocks are skipped, and the else block runs to print what the user entered.
- 2End-of-file signal
If the user presses Ctrl+D, or Ctrl+Z on Windows, the
raw_inputcall raises EOFError. Python selects the first except block because it matches EOFError. - 3Cancellation signal
If the user presses Ctrl+C, a KeyboardInterrupt is raised. Python skips the first handler, selects the second except block, and runs its custom message.
- 4Another exception
If an exception other than the listed types occurs, neither of those except blocks matches. If no exception occurs, the except blocks are also skipped.
The selected path depends on what happens during the input operation: the else path is used for normal completion, while the first matching specific handler is used for EOFError or KeyboardInterrupt.
Place except blocks from most specific to most general. Because Python checks them in order and stops at the first match, a general handler placed first can catch everything and prevent more specific handlers below it from running.
Mistakes in Exception Flow
Common mistake Expecting the rest of the try block to continue after an exception.
What it looks likeA statement raises an exception, but the learner expects later statements in the try block to run.
Why it failsPython immediately stops executing the remaining statements in the try block.
Do this insteadTrace from the raising statement directly to the except-block checks.
Common mistake Expecting every except block to run.
What it looks likeTwo except blocks are written, so the learner expects both handlers to execute.
Why it failsPython runs only the first except block whose exception type matches and skips the others.
Do this insteadCheck the handlers from top to bottom and stop at the first match.
Common mistake Expecting else to run after an exception is handled.
What it looks likeAn exception is caught, and the learner expects the else block to execute afterward.
Why it failsThe else block runs only when no exception occurs in the try block.
Do this insteadUse the else path only for successful completion of the try block.
Common mistake Putting a general exception handler before a specific handler.
What it looks likeA general handler appears before a handler intended for one particular exception type.
Why it failsThe general handler can match first, so the more specific handler below it never runs.
Do this insteadOrder handlers from most specific to most general.
Practice the Execution Path
For the source input example, describe the path for each situation: normal user input, Ctrl+D or Ctrl+Z on Windows, Ctrl+C, and an exception other than EOFError or KeyboardInterrupt.
Hints
- First decide whether the try block completes without an exception.
- If an exception occurs, identify its type and check the except blocks from top to bottom.
- Remember that the else block runs only on the no-exception path.
- Normal input follows the try block to the else block.
- EOFError selects the first matching handler.
- KeyboardInterrupt skips the first nonmatching handler and selects the second matching handler.
- A raised exception stops the remaining try statements and skips else.
- Handlers are checked in written order, so specific handlers belong before general ones.
Key Takeaways
- A try block contains normal code, while except blocks contain error-handling paths for specified exception types.
- When a statement in the try block raises an exception, Python stops the rest of that try block and checks except blocks in order.
- Only the first matching except block runs; the remaining handlers are skipped.
- The optional else block runs only when the try block completes without an exception.
- Put specific exception handlers before general handlers so the intended specific handler can run.