Computer Science · Ch 1 — Exception Handling in Python
Process of Handling Exception
Process of Handling Exception
When Python encounters an error during program execution, it does not simply crash. Instead, it creates a special object called the exception object. This object is packed with information about what went wrong — the type of error, the file name where it occurred, and the exact position in the program where the error was detected.
The runtime system then receives this exception object and begins searching for a piece of code that knows how to handle this specific error. The act of creating the exception object and handing it over to the runtime system is called throwing an exception.
The moment an exception is thrown, the normal flow of the program is abandoned. Control jumps immediately away from the statement that caused the error, and the remaining statements in that block are never executed.
The runtime system now searches the entire program for a block of code called an exception handler — code written specifically to deal with the raised exception. This search follows a strict order:
- It first looks inside the method (function) where the error actually occurred.
- If no suitable handler is found there, it moves to the method that called this method.
- This search continues backwards through the chain of method calls, one level at a time.
This entire chain of methods — from the current one all the way back to the starting point of the program — is called the call stack. The runtime system climbs up the call stack, method by method, until it either finds a handler or runs out of methods to check.
When a suitable handler is finally located somewhere in the call stack, the runtime system executes it. This process of executing the correct handler is called catching the exception.
If the runtime system searches every method in the entire call stack and still cannot find an appropriate exception handler, the program execution stops completely — the program terminates with an error message. …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
This flowchart summarises, step by step, what Python's runtime does internally whenever an exception occurs — the process that all of the earlier shell screenshots were actually going through behind the scenes. It starts at the top with "An error encountered in a method", which leads to "Create exception object" and then "Exception is raised"; together these two steps are labelled Throwing Exception in the side annotation — this is the moment an error condition is turned into an actual exception object and thrown.
Next, the runtime system searches for an exception handler in the current method. If one is Found, control goes straight to "Executes the code (Catching an Exception)" — the exception is handled right there. If Not Found, the runtime instead "Searches for methods in call stack in reverse sequence", labelled Forwarding the exception — meaning it walks back up through whichever functions called the current one, looking for a handler at each level. If a handler turns up during that search, it also leads to "Executes the code (Catching an Exception)." If the search reaches the top of the call stack with no handler found anywhere, the program terminates. …