Testing for and responding to specific error messages

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.

Getting ready

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:

Getting ready

How to do it…

Follow these steps to learn how to write code that responds to specific error codes generated by the execution of a geoprocessing tools:

  1. Open the ArcGIS for Desktop help system by navigating to Start | All Programs | ArcGIS | ArcGIS for Desktop Help
  2. Navigate to Geoprocessing | Tool errors and warnings | Tool errors 1-10000 | Tool errors and warnings 701-800.
  3. Select 000735:<value>:Value is required. This error indicates that a parameter required by the tool has not been provided. You'll recall from running this script earlier that we have not provided the buffer distance and the resulting error message generated, as a result, contains the error code that we are viewing in the help system. In the following code, you will find the full text of the error message. Notice the error code:
    ERROR000735:Distance[valueorfield]:Valueisrequired
  4. If necessary, open the C:ArcpyBookCh11ErrorHandling.py file in IDLE.
  5. In your script, alter the 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")
  6. You can check your work by examining the C:ArcpyBookcodeCh11ErrorHandling5.py solution file.
  7. Save and run the script. You should see various messages printed, as follows:
    Error found in Buffer tool
    Distance value not provided for buffer
    Running the buffer tool again with a default distance value
    Buffer complete
    

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset