By default, Python will generate an error message whenever it encounters a problem in your script. These error messages will not always be very informative to the end user who is running the script. However, it is valuable to take a look at these raw messages. In later recipes, we'll use Python error handling structures to get a cleaner look at the errors and respond as required.
In this recipe, we will create and run a script that intentionally contains error conditions. We will not include any geoprocessing or Python exception handling techniques in the script. We are doing this intentionally because we want you to see the error information returned by Python.
Follow these steps to see a raw Python error message, which is generated when an error occurs while a tool is being executed in a script:
C:ArcpyBookCh11ErrorHandling.py
.arcpy
module:import arcpy
arcpy.env.workspace = "c:/ArcpyBook/data"
Buffer
tool. The Buffer
tool requires a buffer distance be entered as one of its parameters. In this code block, we have intentionally left out the distance parameter:arcpy.Buffer_analysis("Streams.shp","Streams_Buff.shp")
C:ArcpyBookcodeCh11ErrorHandling1.py
solution file.Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:program files (x86)arcgisdesktop10.1arcpyarcpyanalysis.py", line 687, in Buffer raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000735: Distance [value or field]: Value is required Failed to execute (Buffer).
This output error message isn't terribly informative. If you are a fairly experienced programmer, you'll generally be able to make out what the problem is in this case (it did not include buffer distance). However, in many cases, the returned error message will not give you much information that you can use to resolve the problem. Errors in your code are simply a fact of life in programming. However, how your code responds to these errors, also called exceptions, is very important. You should plan to handle errors gracefully through the use of Python error handling structures, which examine arcpy
generated exceptions and act accordingly. Without these structures in place, your scripts will fail immediately, frustrating your users in the process.