PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/error-handling.rst

https://github.com/chadcooper/argis-python-2013
ReStructuredText | 131 lines | 93 code | 38 blank | 0 comment | 0 complexity | 2f50a25c4a346f85b7182ca3b643c24e MD5 | raw file
  1. Error handling
  2. ==============
  3. If you set it up from the beginning, it will make things much easier when
  4. testing your code.
  5. Simple error handling
  6. ---------------------
  7. The basics of error handling are this:
  8. .. code-block:: python
  9. try:
  10. do something...
  11. except:
  12. handle error...
  13. finally:
  14. clean up...
  15. In the ArcMap Python prompt, type this:
  16. .. code-block:: python
  17. arcpy.Buffer_analysis("Observer")
  18. And you'll get something messy like:
  19. .. code-block:: python
  20. Runtime error Traceback (most recent call last): File "<string>",
  21. line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\
  22. arcpy\analysis.py", line 687, in Buffer raise e ExecuteError: ERROR
  23. 000732: Input Features: Dataset Observer does not exist or is not supported
  24. ERROR 000735: Distance [value or field]: Value is required
  25. Now try this:
  26. .. code-block:: python
  27. try:
  28. arcpy.Buffer_analysis("Observer")
  29. except Exception as e:
  30. print e.message
  31. And you get:
  32. .. code-block:: python
  33. ERROR 000732: Input Features: Dataset Observer does not exist or is not supported
  34. ERROR 000735: Distance [value or field]: Value is required
  35. Cleaning up with ``try/except/finally``
  36. ---------------------------------------
  37. Sometimes you want to clean up when something goes south, the ``finally``
  38. clause lets you do that:
  39. .. code-block:: python
  40. try:
  41. if arcpy.CheckExtension("3D") == "Available":
  42. arcpy.CheckOutExtension("3D")
  43. arcpy.Slope_3d("Magic.gdb/NWA10mNED", "Magic.gdb/SlopeNWA")
  44. except Exception as e:
  45. print e.message
  46. print arcpy.GetMessages(2)
  47. finally:
  48. # Check in the 3D Analyst extension
  49. arcpy.CheckInExtension("3D")
  50. And you get:
  51. .. code-block:: python
  52. ERROR 000865: Input raster: Magic.gdb/NWA10mNED does not exist.
  53. .. note::
  54. Using ``finally`` also works well for closing database connections.
  55. Getting stack info with ``traceback``
  56. -------------------------------------
  57. The ``traceback`` module provides a standard interface to extract, format and
  58. print stack traces of Python programs. It exactly mimics the behavior of the
  59. Python interpreter when it prints a stack trace. Basically, it gives you the
  60. most info you can get related to your error(s):
  61. Paste this into a IDLE script and run it:
  62. .. code-block:: python
  63. import sys
  64. import traceback
  65. try:
  66. # Your code goes here
  67. float("a string")
  68. except:
  69. # Get the traceback object
  70. tb = sys.exc_info()[2]
  71. tbinfo = traceback.format_tb(tb)[0]
  72. # Concatenate information together concerning the error into a message string
  73. # tbinfo: where error occurred
  74. # sys.exc_info: 3-tuple of type, value, traceback
  75. pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
  76. msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
  77. # Return python error messages for use in script tool or Python Window
  78. arcpy.AddError(pymsg)
  79. if arcpy.GetMessages(2):
  80. arcpy.AddError(msgs)
  81. print msgs
  82. # Print Python error messages for use in Python / Python Window
  83. print pymsg + "\n"
  84. This tells you **exactly** where your error is occurring:
  85. .. code-block:: python
  86. >>>
  87. PYTHON ERRORS:
  88. Traceback info:
  89. File "C:/Users/class5user/ar-gis-python/crap.py", line 7, in <module>
  90. float("a string")
  91. Error Info:
  92. could not convert string to float: a string
  93. >>>