iNTERFACEWARE Products Manual > Learning Center > Learning Python > Error Detection > Exception Handling |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
Python allows you to create an exception handler to detect a runtime error and keep it from terminating your program. To create an exception handler, use the try and except statements. Here is a simple example of a program that uses an exception handler to catch a division by zero error:
In this example, the try statement specifies the code that might contain the division by zero error, and the except statement specifies the code that is executed if the error is detected. Indenting indicates which statements are enclosed by try and except. If you run this example program, its output looks like this:
Here is what is happening:
This exception handler protects the example program from division by zero errors. The program does not terminate if this error is detected.
|