Skip to content

Instantly share code, notes, and snippets.

@mfunk
Created April 13, 2016 16:25
Show Gist options
  • Save mfunk/7a047c62fd04d82b1a422a98e8eea8ba to your computer and use it in GitHub Desktop.
Save mfunk/7a047c62fd04d82b1a422a98e8eea8ba to your computer and use it in GitHub Desktop.
Small exception block for ArcGIS geoprocessing tools
import sys
import traceback
except arcpy.ExecuteError:
# Get the tool error messages
msgs = arcpy.GetMessages()
arcpy.AddError(msgs)
print(msgs)
except:
# Get the traceback object
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
# Concatenate information together concerning the error into a message string
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + \
"\nError Info:\n" + str(sys.exc_info()[1])
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n"
# Return python error messages for use in script tool or Python Window
arcpy.AddError(pymsg)
arcpy.AddError(msgs)
# Print Python error messages for use in Python / Python Window
print(pymsg + "\n")
print(msgs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment