Skip to content

Instantly share code, notes, and snippets.

@pmacMaps
Last active July 17, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmacMaps/816697b2c4aff0cd7b578056bbd69f41 to your computer and use it in GitHub Desktop.
Save pmacMaps/816697b2c4aff0cd7b578056bbd69f41 to your computer and use it in GitHub Desktop.
Python module to use with ArcPy custom geoprocessing tools that provides an error message including system message, line number, and file of error
# this is the file where the logic for your custom tool goes
# import modules
# import errorLogger.py module
import arcpy, sys, errorLogger
# run your tool's code
try:
pass
# your code goes here
# If an error occurs running geoprocessing tool(s) capture error and write message
# handle error outside of Python system
except EnvironmentError as e:
arcpy.AddError('\nAn error occured running this tool. Please provide the GIS Department the following error messages:')
# call error logger method
errorLogger.PrintException(e)
# handle exception error
except Exception as e:
arcpy.AddError('\nAn error occured running this tool. Please provide the GIS Department the following error messages:')
# call error logger method
errorLogger.PrintException(e)
# final clean up actions for script
finally:
pass
# do whatever else needs to be done
# import modules
import sys, linecache, arcpy
# Function to handle errors
def PrintException(error):
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
# add error message with the system error message, line number of error, and the file that the error ocured in
arcpy.AddError('\nerror: {}\nFILE: {}, LINE: {}\n\n\t "{}": {}'.format(error, filename, lineno, line.strip(), exc_obj))
# exit Python
sys.exit()
# end PrintException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment