These cookies ensure basic functionalities and security features of the website, anonymously. Necessary cookies are absolutely essential for the website to function properly. This results in an exception of dividing by zero: Make sure no divisions by 0 are made. Now, let’s define x and y, but let’s make y zero: try: Print("Make sure both numbers are defined.")Īs x and y are not defined, there will be an error message thanks to our careful error handling: Make sure both numbers are defined Print("Make sure no divisions by 0 are made.") Thus, we need two except blocks for the different cases. So in the try-except structure, we want to handle both exceptions separately. The latter issue causes an ZeroDivisionError. One or both of the numbers are not defined.Before doing this, there are two things that can go wrong: The second and third examples are not specifically caught and print only the traceback information.In Python, you can have multiple except blocks for handling different types of errors separately.įor instance, let’s divide two numbers by one another. The first is a geoprocessing tool error, which prints out the traceback information and the geoprocessing error messages.
The table below shows the expected errors that result from three different lines of codes that could be substituted into the code above. Traceback object and prints the appropriate system error Instead of printing geoprocessing messages, it gets a Type of error occurred, the second except statement would be used. If the same code was used, but a different This statement would print the error messages using If the above code was used and a geoprocessing tool error occurred,Īrcpy.ExecuteError, and the first except statement wouldīe used. # Print Python error messages for use in Python / Python window # Return Python error messages for use in script tool or Python window Msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n" Pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()) # Concatenate information together concerning the error into a message string # Print tool error messages for use in Python # Return tool error messages for use with a script tool Note that the except block is only executed if Buffer returns an error.Ī = "C:/Data/myData.gdb"Īrcpy.CreateSpatialReference_management() Instead of failing without explanation, the except statement is used to trap the error, then fetch and print the error message generated by Buffer. In the following code, Buffer fails because the required buffer_distance_or_field argument has not been provided. Using a basic except statement is the most basic form of error handling. If an error occurs within the try statement, an exception is raised, and the code under the except statement is executed. try-except statementĪ try-except statement can be used to wrap entire programs or just particular portions of code to trap and identify errors. These ID codes have been documented to provide additional information on their cause and how they can be dealt with.
Geoprocessing tool error messages are accompanied by a six-digit code. Use error-handling routines to manage errors and improve a script's usability. If a script does not have an error-handling routine, it fails immediately, which decreases its robustness. In this error-handling routine, retrieve the error message from ArcPy and react accordingly. Python allows you to write a routine that automatically runs when a system error is generated. When a tool writes an error message, ArcPy generates an arcpy.ExecuteError exception. The following sections offer a few techniques that introduce the basics of Python exception handling. These also need to be caught and dealt with in an appropriate manner. Of course, a script can fail for other reasons not related to a geoprocessing tool. In Python, you can provide a variety of structures and methods that can handle exceptions. When a tool returns an error message, ArcPy generates a system error or exception. Writing scripts that expect and handle errors can save time and frustration.