Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Question: Can print statemnts raised from a custom exception be displayed in the gp results window?

Background:

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):
    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_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.zoomToSelectedFeatures()
    df.scale = 8000
    arcpy.RefreshActiveView()

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
share|improve this question

1 Answer 1

up vote 11 down vote accepted

I think you're looking for arcpy.AddMessage.

share|improve this answer
1  
That is it. Thanks. I think that I am going to use arcpy.AddError –  joe Jan 6 '11 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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