Select to view content in your preferred language

Custom Error Handler

459
1
12-12-2012 06:53 AM
SuzanneRoulston-Doty
Emerging Contributor
I am a novice at programming but have been successful at writing some python code.  I am trying to write a custom error handler and have the error msg displayed in the in tool's process dialog box.  Message will print in python shell but not in process dialog box when run from a tool.

# import the required modules for the python script
import arcpy, os, sys, traceback

class NoCaseFld(Exception):
    pass

# define common variables here

caseFlds = "lU_CURRENT;TRACT"             
caseOrder = "tract lucurrent"
orderErr = ""
fldFound = 1


try:

    # 1. If case field(s) selected, check for a specified sort order and create list object
    if caseFlds != "":
        if caseOrder != "":
            orderUpper = caseOrder.upper()
            if " " in orderUpper:
                orderList = orderUpper.split(" ")
            elif "," in orderUpper:
                orderList = orderUpper.split(",")
            elif ";" in orderUpper:
                orderList = orderUpper.split(";")
            else:
                fldFound = -1
                orderErr = caseOrder   #error in reading case field order


        # i. Verify sort order list values are found in case field(s) list       
        caseUpper = caseFlds.upper()
        for f in orderList:
            for c in caseUpper.split(";"):
                if c == f:
                    fldFound = 1
                    continue
                else:
                    fldFound = -1
           
            if fldFound == -1:
                orderErr = orderErr + f  


        if fldFound == -1:
            raise NoCaseFld

except NoCaseFld:
    print "Error: Case Field Name Order\nfield name(s) not found in table:\n" + orderErr
                   


except:                             #error handeling code

    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    arcpy.AddError(msgs)
    arcpy.AddError(pymsg)

    print msgs
    print pymsg
   
    arcpy.AddMessage(arcpy.GetMessages(1))  
    print arcpy.GetMessages(1)
Tags (2)
0 Kudos
1 Reply
SuzanneRoulston-Doty
Emerging Contributor
I finally found another post that answered my question...  Error handling with Python script tools posted Feb 2011 for anyone having this same issue
0 Kudos