Exercise 7
Debugging and error handling
Exercise data
Exercise data for this book can be downloaded from [Link]
This is a link to the ArcGIS® Online group called Python Scripting for ArcGIS Pro (Esri Press). The
data for exercise 7 is posted as a zip file called PythonScripting_Ex07_Data.zip. Download this
file and extract it to a folder of your choice. The instructions use a folder called C:\PythonPro,
but you can use a different folder provided you update any paths.
Examine syntax errors and exceptions
Before starting to work with the exercise data, you will preview the data in ArcGIS® Pro.
When starting a new project in ArcGIS Pro, you can leave the home folder, default geodatabase,
and default toolbox to their original settings or use the exercise folder—e.g.,
C:\PythonPro\Ex07). Saving your work is optional.
1. Start ArcGIS Pro with a new empty project.
2. Make sure the Catalog pane is visible by clicking Catalog Pane on the View tab.
Dock the Catalog pane to the right side of the ArcGIS Pro interface.
3. Create a new folder connection to the location of the exercise data by right-
clicking Folders > Add Folder Connection and navigating to the folder—e.g.,
C:\PythonPro\Ex07.
4. Examine the contents of this folder.
Notice that there are four shapefiles, including point, polyline, and polygon shapefiles,
plus one empty file geodatabase. There are also two scripts in the C:\PythonPro\Ex07folder,
which are not visible in ArcGIS Pro. One of these scripts contains errors, which you will fix in this
exercise.
Syntax errors prevent code from being executed. In the following examples, you will
identify some common syntax errors.
5. Start IDLE.
6. Click File > Open, navigate to the C:\PythonPro folder, and open the script
fc_list.py.
This script has several syntax errors. You may be able to identify them directly, but even
so, it is useful to see how they can be found.
7. Click Run > Check Module.
This brings up a syntax error message: invalid syntax. A red bar in the script shows the
location of the error.
The message reports where the error occurs and that it is a syntax error. You still must
identify the exact nature of the syntax error. In this case, the error is a missing colon at the end
of the line of code.
8. Correct the code on line 4 as follows:
for fc in fclist:
9. Save and run the script.
The script runs but an error message appears in the interactive interpreter, as shown in
the figure.
An AttributeError exception was raised at line 5 in the script. The module arcpy does
not have an attribute named describe. The correct spelling of the function is Describe.
Remember that Python is case sensitive, for the most part.
10. Correct the code on line 5 as follows:
desc = [Link](fc)
11. Save and run the script.
Again, an error message appears in the interactive interpreter:
A NameError exception was raised at line 6 in the script. The name des is not defined.
The correct spelling of the variable is desc.
12. Correct the code on line 6 as follows:
print(f'{desc["baseName"]}: {desc["shapeType"]}')
13. Save and run the script.
This time the script runs correctly, and the result is printed to the interactive
interpreter:
bike_routes: Polyline
county: Polygon
facilities: Point
parks: Polygon
This example illustrates some of the most common errors in Python scripts:
punctuation, capitalization, and spelling.
14. Save and close the script.
Handle exceptions
Many different types of errors can occur when running a script. Rather than just letting
a script cause a runtime error, you can gain more control using certain error-handling
procedures. The most widely used error-handling technique uses a try-except statement.
1. Click File > Open, navigate to the C:\PythonPro folder, and open the script
copy_fcs.py.
The script creates a list of feature classes in a workspace and copies each feature class
to a file geodatabase [Link]. But what if the geodatabase [Link] does not exist? Before
examining that scenario, you will first run the script without errors.
2. Review the path used in line 3 of the code to specify the workspace. If
necessary, modify this path to represent the location of the datasets for this
exercise.
3. Save and run the script.
4. Return to ArcGIS Pro, and confirm that the four feature classes have been
copied to the [Link] geodatabase.
Now that you have confirmed that the script works as intended, we'll introduce an
error.
5. Modify line 5 of the script as follows:
fgdb = "[Link]"
This geodatabase does not exist in the workspace.
6. Save and run the script.
This produces a lengthy error message printed to the interactive interpreter.
The error message includes several references to scripts that are part of ArcPy, and it is
a bit difficult to interpret. However, the end of the message reads as follows:
ExecuteError: ERROR 000210: Cannot create output
[Link]\bike_routes
Failed to execute (CopyFeatures).
The rest of the error message is a bit confusing, which has resulted from the script being
interrupted midprocess. Next, you will trap this error using a try-except statement to gain
control of the error messages.
7. Modify the code as follows:
import arcpy
import os
[Link] = "C:/PythonPro/Ex07"
[Link] = True
fgdb = "[Link]"
try:
fclist = [Link]()
for fc in fclist:
desc = [Link](fc)
outfc = [Link](fgdb, desc["baseName"])
arcpy.CopyFeatures_management(fc, outfc)
except [Link]:
print([Link](2))
except:
print("There has been a nontool error.")
8. Save and run the script.
This time the script runs successfully, meaning that it finishes and is not interrupted
midprocess by an error. The incorrect file geodatabase has not been fixed so there is still an
error, but the error is caught using the try-except statement and only the specific error
message prints.
Although the specific error message is the same as before, there is an important
difference: despite the error, the script ran successfully rather than resulting in a runtime error.
This distinction is important. Say, for instance, that you called this script as a tool in ArcGIS Pro.
If the script results in a runtime error, you may not find out what happened because the printed
error message does not appear anywhere. With the use of the try-except statement, the script
runs successfully, and the error message can be reported back to the tool that called the script.