iNTERFACEWARE Products Manual > Learning Center > Learning Python > Error Detection > Error Detection Details > Exception Hierarchies |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
If a runtime error occurs in a function, Python checks whether an exception handler has been created in the function to handle this error. If no exception handler is defined inside the function, Python checks whether an exception handler is defined in the code that called the function. Here is an example that illustrates this concept:
In this example, the function divide_x_y() detects type mismatch errors, but ignores division by zero errors. The code that calls divide_x_y() contains its own exception handler; this handler detects the division by zero and displays an error message. If an error is detected both inside and outside a function, the exception handler inside the function is used:
Here, the division by zero error is handled inside divide_x_y(). This means that the exception handler outside the function will never be executed. If you want to handle an error both inside and outside a function, use the raise statement:
The raise statement regenerates the division by zero error and passes it up to the code that called the divide_x_y() function. This means that both error messages are displayed.
|