iNTERFACEWARE Products Manual > Learning Center > Learning Python > Error Detection > Error Detection Details > Multiple Exceptions |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
If your code is capable of generating more than one kind of error, your exception handler can provide an except statement for each error you want to detect. For example:
The exception handler in this example can detect two kinds of errors:
In this example, each of the calls to divide_x_y() generates one of the two errors. The resulting output is:
If you want to make sure you are catching every possible error, you can provide an except statement with no error name:
You can also determine the error type and display the error message for any unknown errors you catch. For example, here is the divide_x_y() function, rewritten to obtain the error type and error message:
Here, the Exception exception handler catches all errors not previously caught. This code displays the following when executed:
The error type is obtained from the first element of the list returned by the Python function sys.exc_info(); in this case, the error type is TypeError. The associated error message is stored in errmsg, and is unsupported operand type(s) for /: 'str' and 'int'. |