All errors and warnings generate a specific error code. It is possible to check for specific error codes in your scripts and perform some type of action based on these errors. This can make your scripts even more versatile.
All errors and warnings generated by a geoprocessing tool contain both a six-digit code and a description. Your script can test for specific error codes and respond accordingly. You can get a listing of all the available error messages and codes in the ArcGIS for Desktop help system by navigating to Geoprocessing | Tool errors and warnings. This is illustrated in the following screenshot. All errors have a unique page that briefly describes the error by the code number:
Follow these steps to learn how to write code that responds to specific error codes generated by the execution of a geoprocessing tools:
ERROR000735:Distance[valueorfield]:Valueisrequired
C:ArcpyBookCh11ErrorHandling.py
file in IDLE.except
statement so that it appears as follows:import arcpy try: arcpy.env.workspace = "c:/ArcpyBook/data" arcpy.Buffer_analysis("Streams.shp", "Streams_Buff.shp") except: print("Error found in Buffer tool ") errCode = arcpy.GetReturnCode(3) if str(errCode) == "735": print("Distance value not provided ") print("Running the buffer again with a default valuevalue ") defaultDistance = "100 Feet" arcpy.Buffer_analysis("Streams.shp", "Streams_Buff", defaultDistance) print("Buffer complete")
C:ArcpyBookcodeCh11ErrorHandling5.py
solution file.Error found in Buffer tool Distance value not provided for buffer Running the buffer tool again with a default distance value Buffer complete
What you've done in this code block is use the arcpy.GetReturnCode()
function to return the error code generated by the tool. Then, an if
statement is used to test whether the error code contains the 735
value, which is the code that indicates that a required parameter has not been provided to the tool. You then provided a default value for the buffer distance and called the Buffer
tool again, providing the default buffer value this time.