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 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):

# Import arcpy module
import arcpy, sys, string
from arcpy import env

    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_Layer_Name = Expression
    Output_Layer = "ParcelSelection"

    # Process: Select Layer By Attribute
    arcpy.SelectLayerByAttribute_management("2009 Parcel Points", "NEW_SELECTION", Expression)

    # Zoom to Selected Features
    mxd = arcpy.mapping.MapDocument('CURRENT') 
    df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0] 
    df.scale = 8000

except BadInputError:
    print "Too many characters.  Run again"

except Exception, e:
    # If an error occurred, print line number and error message  
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

1 Answer 1


I think you're looking for arcpy.AddMessage.

The four ArcPy functions for writing messages are as follows:

  • AddMessage("message")—For general informative messages (severity = 0).
  • AddWarning("message")—For warning (severity = 1).
  • AddError("message")—For errors (severity = 2).
  • AddIDMessage(MessageType, MessageID, AddArgument1, AddArgument2)—Used for both errors and warnings (the MessageType argument determines severity).
  • 1
    That is it. Thanks. I think that I am going to use arcpy.AddError
    – joe
    Jan 6, 2011 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.