Python Arcgis Error Handling
Contents |
for Developers Tools to build location-aware apps ArcGIS Solutions Free template maps and apps for your industry ArcGIS Marketplace Get apps and data for your organization Documentation Pricing Support arcpy, error Esri Sign In user My Profile Sign Out Go ArcGIS Pro HomeGet StartedHelpTool
Arcpy Error 000732
ReferenceArcPySDKCommunity Error handling with Python try-except statementraise statementExecuteError classtraceback Getting error messages from a result object Errors happen. Writing scripts
Arcpy Add Warning
that expect and handle errors can save time and frustration. When a tool returns an error message, ArcPy generates a system error or exception. In Python, you can provide a variety of
Arcpy Logging
structures and methods that can handle exceptions. Of course, a script can fail for other reasons not related to a geoprocessing tool. These also need to be caught and dealt with in an appropriate manner. The following sections offer a few techniques that introduce the basics of Python exception handling.When a tool writes an error message, ArcPy generates an arcpy.ExecuteError exception. Python allows you to write arcpy update cursor a routine that automatically runs when a system error is generated. In this error-handling routine, retrieve the error message from ArcPy and react accordingly. If a script does not have an error-handling routine, it fails immediately, which decreases its robustness. Use error-handling routines to manage errors and improve a script's usability.Geoprocessing tool error messages are accompanied by a six-digit code. These ID codes have been documented to provide additional information on their cause and how they can be dealt with.try-except statementA try-except statement can be used to wrap entire programs or just particular portions of code to trap and identify errors. If an error occurs within the try statement, an exception is raised, and the code under the except statement is executed. Using a basic except statement is the most basic form of error handling.In the following code, Buffer fails because the required buffer_distance_or_field argument has not been provided. Instead of failing without explanation, the except statement is used to trap the error, then fetch and print the error message generated by Buffer. Note that the except block is only executed if Buffer returns an error.import arcpy import sys Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site python error handling About Us Learn more about Stack Overflow the company Business Learn more about hiring python traceback developers or posting ads with us Geographic Information Systems Questions Tags Users Badges Unanswered Ask Question _ Geographic Information Systems python print exception Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can http://pro.arcgis.com/en/pro-app/arcpy/get-started/error-handling-with-python.htm answer The best answers are voted up and rise to the top Error handling with python script in ArcGIS 10 up vote 10 down vote favorite 3 I have a python script that I am using to create a list of all mxd files (with full pathing) in our Projects folder. The script then uses that to iterate through the list and do a findandreplaceworkspacepaths on each mxd http://gis.stackexchange.com/questions/20906/error-handling-with-python-script-in-arcgis-10 per ESRI's how to. I am running into problems when I hit a corrupted mxd file. The have tried try/except and haven't gotten it to work. The ideal situation would be to write the corrupt filename to a file and move on so I can come back to them at the end. I'm very new with python scripting, any help would be greatly appreciated. import arcpy, os, sys, traceback, time oldpath = 'W:' newpath = 'W:\\GIS' def find(path,pattern): matches = [] for r,d,f in os.walk(path): for files in f: if files.endswith(pattern): fpath = os.path.join(r,files) matches.append(fpath) print (fpath) return matches print ("Go: ") mxdlist = (find('C:\\gis','.mxd')) print (mxdlist) print ("Starting Path Conversion") try: for mxdold in mxdlist: mxd = arcpy.mapping.MapDocument(mxdold) mxd.findAndReplaceWorkspacePaths(oldpath, newpath) time.sleep(6) mxd.save() time.sleep(6) print (mxdold) del mxd except arcpy.ExecuteError: arcpy.AddError(arcpy.GetMessages(2)) except: arcpy.AddError("Non-tool error occurred") arcpy arcgis-10.0 share|improve this question edited Aug 18 at 9:09 PolyGeo♦ 39.3k1358149 asked Feb 27 '12 at 12:57 bworthington 534 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote First thing to do is bring the top of your for loop outside the try statement. You want to get into your iteration before you give the try statement a chance to kill you Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about http://gis.stackexchange.com/questions/4918/displaying-raised-error-messages-to-user-in-arcgis-geoprocessing-results-window Stack Overflow the company Business Learn more about hiring developers or posting ads with us Geographic Information Systems Questions Tags Users Badges Unanswered Ask Question _ Geographic Information Systems Stack Exchange is a question and answer http://gis.humboldt.edu/OLM/GSP_318/05_2_CallingArcGIS_FromPython.html site for cartographers, geographers and GIS professionals. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise error handling to the top Displaying raised error messages to user in ArcGIS Geoprocessing Results window? up vote 13 down vote favorite 1 Can print statements raised from a custom exception be displayed in the Geoprocessing Results window? I modified a script that I found that takes two inputs from a user, finds the associated feature, then zooms to the feature. The tool works fine. I am trying to make it more user-friendly by automatically python arcgis error formatting the inputs to have the correct amount of characters. I added a messagebox that tells the user if they have too many characters in the input box, but I used easygui to create that (a Python library for GUIs). Instead, I would like to use the gp results window to display the message. I thought that I formatted the raise and except statements correctly but the tool runs and I get a completed result and not my error message. I tried the arcpy help from esri and did not get the results I was looking for. class BadInputError(Exception): pass # Import arcpy module import arcpy, sys, string from arcpy import env try: def checkInput(inp): mNCount = len(inp) trigger = 0 while trigger == 0: if mNCount == 4: trigger = 1 elif mNCount > 4: inp = '' trigger = -1 raise BadInputError elif mNCount == 3: inp = '0' + inp trigger = 1 elif mNCount == 2: inp = '00' + inp trigger = 1 elif mNCount == 1: inp = '000' + inp trigger = 1 return inp # Script arguments MapNumber = arcpy.GetParameterAsText(0) MapNumber = checkInput(MapNumber) ParcelNumber = arcpy.GetParameterAsText(1) ParcelNumber = checkInput(ParcelNumber) Expression = (""" "MAP" = '%s' AND "PARCEL" = '%s'""") %(MapNumber,ParcelNumber) # Local variables: GISData_GISDATA_CAD_PARCEL2009_WHOLE = "Database Connections\\GISUser.sde\\GISData.GISDATA.CAD_PARCEL2009_WHOLE" Output