Exploring the default Python error message

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.

Getting ready

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.

How to do it…

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:

  1. Open IDLE and create a new script.
  2. Save the script to C:ArcpyBookCh11ErrorHandling.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace:
    arcpy.env.workspace = "c:/ArcpyBook/data"
  5. Call the 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")
  6. You can check your work by examining the C:ArcpyBookcodeCh11ErrorHandling1.py solution file.
  7. Run the script. You should see the following error message:
    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).
    

How it works…

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.

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

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