Monday, December 19, 2011

Exit that Script, and exit it now!

Help, I need to kill my script!  Use the sys.exit() gives me an error!!  What to do?

Use the try/except method to just move beyond those errors my friend.  Very simple.


import arcpy
from arcpy import env
import sys
value = arcpy.GetParameterAsText(0)
try:
    arcpy.AddMessage(value)
    arcpy.AddMessage("VERY INTERESTING VALUE....")
    arcpy.AddMessage("FORCED EXIT")
    sys.exit(0)
except SystemExit:
    arcpy.AddMessage("WHEN I EXIT I GO HERE: SystemExit exception")
    pass
except:
    arcpy.AddError("ERROR ERROR" )
    arcpy.AddError(arcpy.GetMessages(2))


Enjoy!

*You can also use:
import os
os_exit(0)
I haven't tested this though.