Skip to content
All apps

Where Does the Exception Go?

About this app

Deep inside, an exception arises and visibly rises frame by frame through the call stack until a matching except catches it or the program aborts. Every frame it leaves writes its line into the growing traceback; at each net the app checks the type (“ValueError matches except ValueError: yes”). Four programs from real Python 3.13 (price list, order, run, missing file), switches for try, else/finally and where to catch, and first the question “Who catches the exception?”.

How exceptions propagate

When an error occurs in Python, for example a ValueError from float("3,50"), an exception is raised. The running function stops and the exception rises to its caller, then to that caller's caller and so on. If a call sits inside a try block with a matching except, the exception is caught there and the program continues in the except block. An except matches if its type is the same or a superclass. If nobody catches it, the program stops with a traceback listing the frames that were left. else runs only without an exception, finally always.

What the app shows

On the left the code board with a console, on the right the call stack with one frame per running call and its local names. Under frames inside a try hangs a safety net with their except blocks. The exception flies upwards as a card; every frame it leaves turns grey and writes its line into the traceback. At the net the app checks the type, for example “ValueError matches except ValueError: yes”. An explanation translates the error message, and a panel shows the hierarchy of exception classes.

Try it yourself

There are four programs: a price list, an order, a run and a missing file. Switch try on or off, move the catching point, add else and finally and change the data. Before the catch the app asks: who catches the exception? Afterwards you see whether your prediction was right, and you can copy every program and run it yourself.

Frequently asked questions

What happens when an exception is not caught in Python?
It rises through all waiting function calls up to the main program. If nobody catches it there either, the program stops and prints a traceback: the header Traceback (most recent call last), the calls involved from outside to inside, and finally the type and message of the exception.
When does an except block match an exception?
When the exception is an instance of the given class or one of its subclasses. except LookupError therefore also catches KeyError and IndexError, and except Exception catches almost everything. Python checks the except blocks from top to bottom, and the first match wins.
What is the difference between else and finally in try?
The else block runs only if no exception occurred in the try block. The finally block always runs, with or without an exception and even when the exception keeps propagating. It is the place for clean-up, such as closing a file.
What does Where does the exception fly? show?
How an exception rises through the call stack in four Python programs, where it is caught and how the traceback builds up. try, else, finally, the catching point and the data can be switched, and a prediction question comes before the catch.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps