desc0107

Error handling with Python script tools

Discussion created by desc0107 on Jul 18, 2011
Latest reply on Aug 3, 2011 by lpugh01
I am trying to integrate some error handling in my python script tools but I am not exactly sure how to do it properly.  The desktop help has a lot of great examples but there are so many different ways of doing this.   I am also a bit confused with the difference between error messaging in standalone script vs script tools??

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000.htm

Below is a subset of my script, only showing the error handling portion.

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
import sys, string, os
arcpy.env.overwriteOutput = True

try:
    # Check out any necessary licenses
    if arcpy.CheckProduct("ArcInfo") == "Unavailable":
        arcpy.AddError("ArcInfo Licensing Issue")
        raise LicenseError
    if arcpy.CheckExtension("Spatial") == "Available":
        arcpy.CheckOutExtension("Spatial")
    else:
        arcpy.AddError("Spatial Analyst Licensing Issue")
        raise LicenseError

# all all the processing here....


except:
    print arcpy.GetMessages(2)
finally:
    # Check in the Spatial Analyst extension
    arcpy.CheckInExtension("Spatial")      


Questions:
-Will the above work properly for script tools?  For the except: block will the print do anything in script tools or do I need to use some other syntax to print the error instead?
-In this particular tool I have a cleanup routine that deletes intermediate files.  I currently have this before the except statement but would like to make sure that if the tool crashes that it cleans-up the intermediate files.  Is it possible to put the cleanup routine within the finally: block?  Is that good coding practice?

Any tips on simple and best practice for error handling in script tools (containing a series of geoprocessing steps) would be greatly appreciated.

Thanks.

Alice

Outcomes