Script tool messages

When a tool is run, ArcPy identifies the application it is called from. This allows you to write messages in Python. Those messages automatically appear on the tool dialog box, in Geoprocessing history, and in the Python window. It also means that any model or script tool that calls your tool has access to the messages you write.

To learn more about messaging, see Understanding messaging in script tools.

After a tool has run, messages can be retrieved with geoprocessing functions. The four ArcPy functions for writing messages are as follows:

FunctionDescription

AddMessage(message)

An informative message is added to the tool's messages.

AddWarning(message)

A warning message is added to the tool's messages.

AddError(message)

An error message is added to the tool's messages.

AddIDMessage(message_type, message_ID, add_argument1=None, add_argument2=None)

A message of any type is added using geoprocessing message codes.

A call to AddIDMessage displays a short message and the message ID, which is a link to an explanation about the cause of and solutions to the problem. When you add an error message using either AddError or AddIDMessage, the following occurs:

  • The script run continues. It is up to you to add appropriate error-handling and stop logic in the script. For example, you may need to delete intermediate files or cursors.
  • Upon return from your script, the calling script or model receives a system error and the tool stops running.

Example of adding messages

In the example below, the input is evaluated, and if it contains no input features, an error message is added to the tool and an arcpy.ExecuteError exception is raised to end the tool.

import arcpy
input = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(0)
       
# If the input has no features, add an error message, and raise
# an arcpy.ExecuteError
if int(arcpy.GetCount_management(input)[0]) == 0:
    arcpy.AddError("{0} has no features.".format(input))
    raise arcpy.ExecuteError

Temas relacionados