tlsilveus

new to python - trouble with error trapping

Discussion created by tlsilveus on Apr 19, 2011
Latest reply on Apr 20, 2011 by tlsilveus
Can someone show me how to trap (and display) errors in python when using as a tool in arcGIS?  I'm use try and except, and when I run the script in a python gui it DOES NOT (edited) display the errors, but when I run in arcGIS it tells it it completed successfully even though it didn't...  The main error  that needs to be trapped is when the program cannot get write access to a dataset via an update cursor.

EDITED: Actually, I just tested again in python gui and I don't get the messages there either.  I must have been getting a traceback call before I trapped the error.  My mistake.  So I'm still left with how do I trap and display an error using python.  My code is below.  I didn't post it originally, but maybe someone can see what I'm doing wrong.

import arcpy

#Initialize variables
searchDSParam = None
searchLinkParam = None
searchFieldParam = None
updateDSParam = None
updateLinkParam = None
updateFieldParam = None

#arcGIS toolbox script
searchDSParam = arcpy.GetParameterAsText(0)
searchLinkParam = arcpy.GetParameterAsText(1)
searchFieldParam = arcpy.GetParameterAsText(2)
updateDSParam = arcpy.GetParameterAsText(3)
updateLinkParam = arcpy.GetParameterAsText(4)
updateFieldParam = arcpy.GetParameterAsText(5)

#for testing
#searchDSParam = "C:\\GIS\\Temp.shp"
#searchLinkParam = "SS"
#searchFieldParam = "POINT_Y"
#updateDSParam = "J:\\Public Works\\Disaster\\2011_MarchFlood\\Flood2011March.shp"
#updateLinkParam = "SS"
#updateFieldParam = "LAT"

updateTheRow = None
updateTheRows = None
searchTheRow = None
searchTheRows = None
updateTheFields = None
searchTheField = None
searchTheFields = None
fldType = None
fldPrecision = None
fldScale = None
fldLength = None
whereClause = None
linkTypeText = False

#Dictionary to traslate return value of field.type to parameter value of arcpy.AddField_management
fieldTypeDictionary = {'SmallInteger': 'SHORT', 'Integer': 'LONG', 'Single': 'FLOAT', 'Double': 'DOUBLE', 'String': 'TEXT', 'Date': 'DATE'}
changed = 0

try:
    #get work done
    #add field to update dataset if necessary
    #search cursor has data to be applied to update cursor (in searchFieldParam field)
    #update cursor has field to be updated (updateFieldParam gets data from searchFieldParam)
    #link update cursor with search cursor on fields searchLinkParam/updateLinkParam

    updateTheFields = arcpy.ListFields(updateDSParam) #get a list of fields in shapefile to be updated
    searchTheFields = arcpy.ListFields(searchDSParam) #get a list of fields in the shapefile to be searched
    for searchTheField in searchTheFields:
        if searchTheField.name.upper() == searchFieldParam.upper():
            fldType = searchTheField.type
            fldPrecision = searchTheField.precision
            fldScale = searchTheField.scale
            fldLength = searchTheField.length
        if searchTheField.name.upper() == searchLinkParam.upper():
            if searchTheField.type == "String":
                linkTypeText = True
            
    #check to see if it has the field that will be updated, if it has it good; if not then add the field
    fieldExists = False
    for fld in updateTheFields:
        if fld.name.upper() == updateFieldParam.upper():
            fieldExists = True
            break
        
    if not fieldExists:
        #field does not exist - need to add field
       arcpy.AddField_management(updateDSParam,updateFieldParam,fieldTypeDictionary[fldType], fldPrecision, fldScale, fldLength) #adds field to data set

    updateTheRows = arcpy.UpdateCursor(updateDSParam) #get the rows from the shapefile that will have a field updated
    #searchTheRows = arcpy.SearchCursor(searchDSParam)
    
    for updateTheRow in updateTheRows:
        #loop through all the rows in the update cursor
        if linkTypeText:
            whereClause = searchLinkParam + " = '" + updateTheRow.getValue(updateLinkParam) + "'"
        else:
            whereClause = searchLinkParam + " = " + str(updateTheRow.getValue(updateLinkParam) )
            
        searchTheRows = arcpy.SearchCursor(searchDSParam,whereClause)
        for searchTheRow in searchTheRows:
            if searchTheRow.getValue(searchLinkParam) == updateTheRow.getValue(updateLinkParam):
                if searchTheRow.getValue(searchFieldParam) <> "":
                    updateTheRow.setValue(updateFieldParam,searchTheRow.getValue(searchFieldParam))
                    updateTheRows.updateRow(updateTheRow)
                    changed = changed + 1
                    break #exit search cursor loop - you found first row with data and updated row
            
        searchTheRows = None
    updateTheRows = None
    print "Records Changed: " + str(changed)
    if changed == 0:
        arcpy.AddWarning( "No Features Modified")
    else:
        arcpy.AddReturnMessage(str(changed) + " Records Changed")
                               
except:
    # Display exceptions
    for msg in range(0, arcpy.GetMessageCount()):
        arcpy.AddReturnMessage(msg)       

finally:
   
    #clean up (close cursors and rows)
    if searchTheRow:
        del searchTheRow

    if updateTheRow:
        del updateTheRow

    if updateTheRows:
        del updateTheRows

    if searchTheRows:
        del searchTheRows

Outcomes