Concepts / Common Exception Types

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.

  • Programming
Interactive lab

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

  1. Statements in try run until one raises an exception; the rest of try is skipped.
  2. Python checks the except clauses in order and runs the first one whose type matches.
  3. If nothing was raised, the else block runs instead.
  4. 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.

Educational simulation

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.

Try blockcompletes normallyElse blockrunsTry blockraises exceptionMatching exceptrunsno exceptionexception raised
What differs between the path where the try block completes and the path where a statement raises an exception?

The Try and Except Structure

Definition

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.

trystarts normal pathTry statementsexpected workExcept blocksspecific handlersElse blockno-exception pathcontainserror pathsuccess path
How are normal code, exception handlers, and optional success-only code connected?
Key point

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.

Try statementbegin executionTry statementexecute in orderRaised exceptionstop try blockRemaining statementsskippedElse blockskippedFollowing codecontinueMatching exceptfirst matchenterraisesskipcheck handlersskipcontinue
What happens next when a statement in the try block raises an exception, and which statements are skipped?

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?

  • They continue running before the except block.

  • They are skipped, and Python checks the except blocks.

  • They run only if the else block is present.

Reveal answer
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

Worked example

Input Cancellation and End of Input

Trace the source example in which an input operation may raise EOFError or KeyboardInterrupt.

  1. 1
    Normal 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.

  2. 2
    End-of-file signal

    If the user presses Ctrl+D, or Ctrl+Z on Windows, the raw_input call raises EOFError. Python selects the first except block because it matches EOFError.

  3. 3
    Cancellation 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.

  4. 4
    Another 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.

Result

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.

Raised exceptionfrom try blockEOFError matchfirst handlerEOFError handlercustom messageKeyboardInterrupthandlercustom messageKeyboardInterruptmatchsecond handlerNo listed matchlisted handlers skippedcheck firstmatchno matchmatchno match
How does Python compare a raised exception with multiple except blocks and decide which handler runs?
Best practice

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 like

    A statement raises an exception, but the learner expects later statements in the try block to run.

    Why it fails

    Python immediately stops executing the remaining statements in the try block.

    Do this instead

    Trace from the raising statement directly to the except-block checks.

  • Common mistake

    Expecting every except block to run.

    What it looks like

    Two except blocks are written, so the learner expects both handlers to execute.

    Why it fails

    Python runs only the first except block whose exception type matches and skips the others.

    Do this instead

    Check 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 like

    An exception is caught, and the learner expects the else block to execute afterward.

    Why it fails

    The else block runs only when no exception occurs in the try block.

    Do this instead

    Use 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 like

    A general handler appears before a handler intended for one particular exception type.

    Why it fails

    The general handler can match first, so the more specific handler below it never runs.

    Do this instead

    Order handlers from most specific to most general.

Practice the Execution Path

MEDIUM

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.

Related concepts

More concepts starting with C