I could use some advice on how to handle errors in my Python script. From what I have been able to gather from reading all of the Python error handling posts on this site, is that you cannot simply bypass an error within a for loop with a try except continue statement. Instead, you have to handle each error directly. This is where I am having problems tying it all together. I have attached the error message that occured in the middle of a for loop. Additionally, I have attached my script which moves through the following workflow:
- place a polygon around the raster
- place a point on the mean center of the polygon
- use the point to identify a specific county associated with its corresponding raster
- clip the raster based on the selected county polygon
How do I incorporate information from the error message into a try except continue statement, so that the script can move to the next raster in the list rather than stopping in the middle of processing?
# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("3D")
# Set Over write
arcpy.env.overwriteOutput = 1
# Set the workspace
env.workspace = r"Z:\temp.gdb"
outworkspace = r"Z:\location2\temp2.gdb"
# Local variables:
counties = r"Z:\temp.gdb\boundaries\Counties"
counties_lyr = arcpy.MakeFeatureLayer_management(counties,"counties_lyr")
# Get the list of rasters to process
raster_list = arcpy.ListRasters("*_clp")
print raster_list
for raster in raster_list:
# Define name and location for output raster
name = outworkspace + "\\" + str(raster)
# Process: Raster Domain
arcpy.RasterDomain_3d(raster, "in_memory/temp", "POLYGON")
# Process: Central Feature
arcpy.MeanCenter_stats("in_memory/temp", "in_memory/temp1")
# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(counties_lyr, "intersect", "in_memory/temp1", "", "NEW_SELECTION")
# Clip Raster
arcpy.Clip_management(raster, "#", name,counties_lyr, "#", "ClippingGeometry")
# Delete in_memory
arcpy.Delete_management("in_memory")
print "processing " + raster + " complete..."
print "All processing is now finished"
