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.

Revisions

  1. mfunk created this gist Apr 13, 2016.
    25 changes: 25 additions & 0 deletions ArcGIS_gp_exception_block_python.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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)