Python Error Handling with
Exceptions
What is an exception?
An exception is a runtime
condition that could cause your program to crash
Almost every modern programming language handles exceptions
This technique is generically referred to as validation
We will look at the Python try�except block that protects your program from crashes
What triggers an exception?
Exceptions can occur automatically
or they can be raised manually
Exceptions that occur automatically are raised by a built-in library
function or operation
For example, the expression:
a = 3 / 0
would raise a ZeroDivisionError exception by the division ( / ) operator
This python module handles simple ZeroDivisionError exceptions
Here is a Python module
(TryExceptExample.py) that handles generic exceptions
Sometimes you will need to raise an exception manually
For example, you might want to exclude integers that fall outside the
data range that is valid for the problem you�re trying to solve
There is no automatic trigger that will make this happen
If you want to limit the data range for a problem to all positive
non-zero integers, you could do this:
>>> a = -1 # set a test value for our variable a
>>> if a < 0:
...�� raise
ValueError("the value of a is less than 0")
...
Traceback (most recent call last):
� File "<stdin>", line 2, in <module>
ValueError: the value of a is less than 0
This Python module
(RaiseValueErrorException.py) uses both automatically and manually generated
exceptions and includes other improvements over TryExceptExample.py
Using try�except blocks
If an exception (runtime error) happens in a try clause, we can handle it
in a corresponding except
clause
try does not take any arguments
except can take a qualifier (one of many exception types)
or not
If it does not take a qualifier, any exception will trigger it
By handling an exception in an except clause, we can take action to correct the error, or
at least prevent an immediate crash
Exception classes
A try�except block can handle many different types of exceptions
Exceptions
are defined as objects of Python classes
Many are �built-in� to a Python language distribution (like Anaconda and
most others)
Typical built-in exception classes include IOError, TypeError, OverflowError, and many others
A single try
clause can be followed by multiple except clauses
Think of the try
clause as a place to put dangerous code
The except
clauses handle specific things that go wrong in the try clause
You can create your own exception classes that inherit from Exception
Best practices with try�except
processing
In general, put code in a try clause if it has the potential to cause a crash
Often this can be due to bad external data values
At best, we want the program to continue working and simply reject the
bad data
At worst, we want it to crash gracefully, giving the user relevant post
mortem information
Be specific�if you can, trap the exact exceptions
RaiseValueErrorException.py does this
TryExceptExample.py does not do this�it traps generic exceptions with except:
Typical uses in an ArcPy script
In a data entry box, you could check to see if a specific type of ArcGIS
data object (like a shapefile) has been entered
If the user were to type in the path to a shapefile, you could verify
that it exists and then tell the user if there was a problem
And a million other things�