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??

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

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

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

# all all the processing here....

    print arcpy.GetMessages(2)
    # Check in the Spatial Analyst extension

-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.