Concepts / Raising Exceptions

Raising Exceptions

If every statement completes without raising an exception, Python skips the except blocks.

  • 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…

When Normal Execution Breaks

A try..except statement separates two responsibilities. The try block contains code that Python should attempt to execute normally. The except blocks contain the error-handling code used if an exception is raised. This separation lets a program respond to particular exceptions instead of treating normal execution and error handling as one undifferentiated sequence.

What do you think happens?

A try block contains several statements. If one statement raises an exception, what happens to the statements that follow it inside the same try block?

  • They all run 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.

When a statement in the try block raises an exception, Python immediately stops executing the rest of that try block and checks the except blocks in order.

The Execution Path

try blockbegin executionstatementraises exceptionexcept blockschecked in ordermatching handlerfirst matching exceptfollowing codeafter try..exceptremaining statementsinside try blockexecuteskipexception raisedfirst matchfinish
What happens next when a statement inside the try block raises an exception, and which statements are skipped?

Python begins with the statements inside the try block and executes them in order. If every statement completes without raising an exception, Python skips the except blocks. If an else block is present, Python then runs it. If a statement raises an exception, Python stops the try block immediately, so later statements in that block do not run. Python checks the except blocks and executes the first one whose exception type matches. After that matching except block finishes, the else block is skipped and execution continues after the entire try..except statement.

Statement Structure

Definition

The try..except structure starts with the try keyword, a colon, and an indented block of statements. One or more except blocks follow. Each except block specifies an exception type to catch and contains the handling statements for that type. An optional else block can follow the except blocks and runs only when the try block completes without an exception.

trystarts protected codetry statementsexpected normal workexceptstarts handlerexception typetype to catchhandler statementsresponse to exceptionelse blockonly without exceptioncontainsfollowed byspecifiescontrolsoptional next block
What code belongs inside the try block, what belongs inside except blocks, and how the blocks are connected?

Choosing a Handler

raised exceptionfrom try blockEOFErrorfirst except typeEOFError handlerfirst matchKeyboardInterruptsecond except typeKeyboardInterrupthandlersecond matchother exceptionno listed matchcheck firstmatchno matchmatchno match
How does Python compare a raised exception with multiple except blocks and choose which handler to run?

Multiple except blocks are checked in the order in which they appear. Python compares the raised exception with the type specified by the first except block. If they match, that handler runs and every later except block is skipped. If they do not match, Python checks the next except block. This continues until a matching handler is found. If no listed handler matches, the except blocks described here are skipped. Because only the first matching handler runs, handlers should be ordered from most specific to most general.

Best practice

Place a more specific exception handler before a more general one. A general handler placed first can match everything covered by the later specific handlers, making those later handlers unreachable during the matching process.

Input Events in Practice

Worked example

Tracing different input outcomes

A program asks the user to enter something. It has one except block for EOFError, a second except block for KeyboardInterrupt, and an else block that prints the entered value.

  1. 1
    Normal input

    The input operation completes without raising an exception. Both 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 input operation raises EOFError. Python stops the try block, selects the first matching except block, and skips the second except block and the else block.

  3. 3
    Cancellation signal

    If the user presses Ctrl+C, a KeyboardInterrupt is raised. The EOFError handler does not match, so Python checks the next except block and runs the KeyboardInterrupt handler. The else block is skipped.

  4. 4
    Another exception

    If a different exception occurs, neither of these two specific except blocks matches. The two handlers and the else block are skipped.

Result

The same try..except structure provides three distinct paths: successful input through else, end-of-file handling through the EOFError handler, and cancellation handling through the KeyboardInterrupt handler.

Try block outcomeExcept blocksElse blockNext execution point
No exceptionAll skippedRunsAfter the try..except statement
Matching exceptionFirst matching block runs; later blocks are skippedSkippedAfter the try..except statement
Exception with no listed matchListed blocks are skippedSkippedNot handled by those listed blocks

Control-flow differences between normal completion and exception paths

Mistakes in Handler Order

  • Common mistake

    Putting a general exception handler before a more specific handler.

    What it looks like

    A general handler appears first, followed by a handler intended for one particular exception type.

    Why it fails

    Python checks handlers in order. The general handler can catch the exception before Python reaches the specific handler.

    Do this instead

    Order multiple except blocks from most specific to most general.

  • Common mistake

    Assuming statements after the raising statement inside the try block will still run.

    What it looks like

    A later statement in the try block is expected to execute after an earlier statement raises an exception.

    Why it fails

    Python immediately stops executing the rest of the try block when an exception is raised.

    Do this instead

    Trace from the raising statement directly to the except-block matching process.

  • Common mistake

    Expecting the else block to run after an exception is handled.

    What it looks like

    An except block handles EOFError, and the else block is also expected to run.

    Why it fails

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

    Do this instead

    Treat else as the successful, no-exception path.

Trace It Yourself

MEDIUM

Suppose a try block contains three statements. The first completes normally, the second raises KeyboardInterrupt, and the third would normally produce a result. There are two except blocks: the first catches EOFError and the second catches KeyboardInterrupt, followed by an else block. Trace the order of execution. Identify which statement is the first skipped statement, which except block runs, whether the else block runs, and where execution continues afterward.

Hints
  • Stop tracing the try block at the statement that raises the exception.
  • Check the except blocks from top to bottom.
  • The else block requires the try block to complete without an exception.

Key Takeaways

  1. 1
    The try block contains statements expected to execute normally; except blocks contain exception-specific handling.
  2. 2
    When a statement raises an exception, the rest of the try block is skipped immediately.
  3. 3
    Python checks multiple except blocks in order and runs only the first matching handler.
  4. 4
    The optional else block runs only when the try block completes without raising an exception.
  5. 5
    Order handlers from most specific to most general so that specific handlers can be reached.

At a glance

  • try..except separates normal execution from error handling.
  • An exception stops the remaining statements in the try block.
  • Except blocks are checked from top to bottom, and only the first matching block runs.
  • else represents the no-exception path and is skipped when an exception occurs.
  • Specific exception handlers should appear before general handlers.

Related concepts

More concepts starting with R