<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Try/Except statement inside of For Loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519522#M40663</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Get rid of the row and cursor object deletes just before the continue that you added.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The deletes are still inside the for loop&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;destroying the cursor there shoots the for loop in the foot;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;it has nowhere to go.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;conceptually, you have:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. get a row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. try something complex&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. if the try fails, write home then kill the cursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-- and where does the next row for the loop come from???&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;get rid of the break and the continue too, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the loop should continue without prompting if you let it alone.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;break and continue are for when there is further processing inside the loop you want to skip,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;or if you want to jump out of the loop early.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;just try using &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;except: &amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;at least just to try it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This should let the loop run.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 22 Jan 2014 16:57:24 GMT</pubDate>
    <dc:creator>markdenil</dc:creator>
    <dc:date>2014-01-22T16:57:24Z</dc:date>
    <item>
      <title>Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519518#M40659</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This script is meant to loop through several records. I a problem arise with processing a record I want the tool to skip that record and loop to the next record. I was working for an error code I was getting, but now I'm getting a new error and the script no longer loops back to&amp;nbsp; process the next record. Any thoughts on what's happening?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is only part of the script that's inside the for loop - &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; import arcpy, os, shutil, datetime from arcpy import env from arcpy.sa import*&amp;nbsp; #set workspaces arcpy.env.workspace = arcpy.GetParameterAsText(0)&amp;nbsp; outputWorkspace = arcpy.GetParameterAsText(1)&amp;nbsp; arcpy.env.overwriteOutput = True&amp;nbsp; #Check out the ArcGIS 3D Analyst extension license arcpy.CheckOutExtension("3D") arcpy.CheckOutExtension("Spatial")&amp;nbsp; arcpy.SetProgressor("default","Conducting Viewshed Analysis")&amp;nbsp; #Variables ObsPts = arcpy.GetParameterAsText(2) footprint =&amp;nbsp; arcpy.GetParameterAsText(3)&amp;nbsp; Elevation = arcpy.GetParameterAsText(4) BareElevation = arcpy.GetParameterAsText(5) Ocean = arcpy.GetParameterAsText(6) FloorField = arcpy.GetParameterAsText(7)&amp;nbsp; Year = arcpy.GetParameterAsText(8)&amp;nbsp; #Set analysis extent to elevation raster arcpy.env.extent = Elevation arcpy.env.cellSize = Elevation&amp;nbsp; #Open error log file infile = open(outputWorkspace+"\\Error_Log_"+Year+".txt","w")&amp;nbsp; #Count number of parcels being processed arcpy.AddMessage("\nCalculating viewshed for "+str(RangeCount)+" parcels")&amp;nbsp; sc = arcpy.SearchCursor(PointsFL)&amp;nbsp; for row in sc:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; try:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Get Parcel ID value &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = row.ID &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = row.FID+1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FlrCnt = row.getValue(FloorField)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Get bare earth elevation of parcel &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetProgressorLabel("Changing elevation footprint to bare earth elevation for point "+str(value)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SQL = "Id =" +str(value)+"" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(PointsFL,"NEW_SELECTION",SQL) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(footprintFL,"INTERSECT",PointsFL)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = IntermediateFiles #need to change workspace so that the .save files get saved correctly &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outExtractByMask = ExtractByMask(BareElevation,footprintFL) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outExtractByMask.save(IntermediateFiles+"\\ebm_"+str(value))&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ElevAvg = ElevAvgTables+"\\avgelev_"+str(value)+".dbf" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Statistics_analysis(outExtractByMask,ElevAvg,[["VALUE","MEAN"]])&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(ElevAvg,"Pt_ID","SHORT") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(ElevAvg,"Pt_ID",value) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddJoin_management(PointsFL,"Id",ElevAvg,"Pt_ID","KEEP_COMMON")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Field1 = os.path.basename(ObsPts).split(".")[0]+".SPOT" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Field2 = "!"+os.path.basename(ElevAvg).split(".")[0]+".MEAN_VALUE!" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(PointsFL,Field1,Field2,"PYTHON_9.3") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RemoveJoin_management(PointsFL)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Set parcel elevation to 0 this will be replaced by SPOT value caclualted above &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RastFootprint = IntermediateFiles+"\\fp_"+str(value).split(".")[0] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.PolygonToRaster_conversion(footprintFL,"FID",RastFootprint,"MAXIMUM_AREA","",6) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outIsNull = IsNull(RastFootprint) #Identify NoData Areas &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outIsNull.save(IntermediateFiles+"\\null1_"+str(value)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outCon = Con(outIsNull,Elevation,0,"") #Use Con tool to change building footprint to elevation of 0 while leaving all other building footprints as is &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outCon.save(IntermediateFiles+"\\con1_"+str(value)) #Final raster to be used in viewshed analysis&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #buffer selected viewpoint &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetProgressorLabel("Buffering point "+str(value)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outBuffer = IntermediateFiles+"\\buffer_"+str(value)+".shp" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(PointsFL,outBuffer,"1 mile")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Convert buffer polygon to line &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BufferLine = IntermediateFiles+"\\BufferLine_"+str(value)+".shp" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.FeatureToLine_management(outBuffer,BufferLine)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Clip buffer to Ocean &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SetProgressorLabel("Clipping point "+str(value)+" buffer to ocean") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BufferClip = IntermediateFiles+"\\buffer_clipped_"+str(value).split(".")[0]+".shp" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_analysis(outBuffer, Ocean, BufferClip)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if FlrCnt ==1: #parcel floor count =1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("\nParcel "+str(value)+" has 1 story to process. Calculating viewshed now...") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nParcel ",str(value)," has 1 story to process. Calculating viewshed now..." &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DegViewshed(1,10) #Calculate the viewshed with an observer height of 10 feet then move to point&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("First floor viewshed for parcel "+str(value)+" has been completed...")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "First floor viewshed for parcel ",str(value)," has been completed..." &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(count)+" of "+str(RangeCount)+"parcles has been completed.\n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print str(count)," of "+str(RangeCount)," parcels has been processed.\n"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: #if parcel has 1.5 floors or greater do this &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("\nParcel "+str(value)+" has 2 stories to process. Calculating viewsheds now...") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nParcel ",str(value)," has 2 stories to process. Calculating viewsheds now..." &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DegViewshed(1,10) #Calculate first floor view, then &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("First floor viewshed for parcel "+str(value)+" has been completed...") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "First floor viewshed for parcel ",str(value)," has been completed..."&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DegViewshed(2,20) #Calculate second floor view &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Second floor viewshed for parcel "+str(value)+" has been completed...")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Second floor viewshed for parcel ",str(value)," has been completed..." &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Viewsheds for "+str(count)+" of "+str(RangeCount)+" parcels have been processed.\n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Viewsheds for",str(count)," of ",str(RangeCount),"parcels have been processed.\n"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; except:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("***An error occured processing parcel "+str(value)+". Refer to error log for details.") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "***An error occured processing parcel "+str(value)+". Refer to error log for details." &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; infile.write("An error occured processing parcel "+str(value)+".\n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; infile.write(arcpy.GetMessages()+"\n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(PointsFL, "CLEAR_SELECTION") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(footprintFL,"CLEAR_SELECTION") &amp;nbsp;&amp;nbsp;&amp;nbsp; del row &amp;nbsp;&amp;nbsp;&amp;nbsp; del sc &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row del sc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jan 2014 16:47:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519518#M40659</guid>
      <dc:creator>RachelAlbritton1</dc:creator>
      <dc:date>2014-01-21T16:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519519#M40660</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Rachel,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm a little unclear as to what's going on. You have an impressively complicated script. I'm sure you're having on hell of a time trying to debug it. Which error message are you currently getting?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think you should get rid of "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;del row and del sc&lt;/SPAN&gt;&lt;SPAN&gt;" from within your for loop. You have it twice. You can't delete &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;sc&lt;/SPAN&gt;&lt;SPAN&gt; when you're still looping through it. Also, the del lines outside the for loop will error out because &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;row&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;sc&lt;/SPAN&gt;&lt;SPAN&gt; will have already been deleted at the end of the for loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Judging by your code, you probably already know this, but there are two python keywords that might help you out. When you add the keyword &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;continue&lt;/SPAN&gt;&lt;SPAN&gt; python will automatically jump to the next item in the current loop. The keyword &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;break&lt;/SPAN&gt;&lt;SPAN&gt; will end the current loop and resume the script after the current loop. If you're working on loops in loops, it will only apply to the most resent loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also note that everything that happens before the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;try&lt;/SPAN&gt;&lt;SPAN&gt; fails is permanent.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, if all your code works up to your last &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;print&lt;/SPAN&gt;&lt;SPAN&gt; line (under &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;try&lt;/SPAN&gt;&lt;SPAN&gt;), which errors out, then your records will still be fully processed. Since there would have techically been an error, the code under &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;except&lt;/SPAN&gt;&lt;SPAN&gt; will also be run.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope to hear back from you soon. Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Jan 2014 18:13:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519519#M40660</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-01-21T18:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519520#M40661</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for such a quick response. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially the issue is the for loop doesn't loop back to the next record if an error occurs with the current record. The script just stops. For example, I'm currently getting an Execute Error for a record (The specific error isn't my concern at the moment - the concern is the ability of the FOR LOOP to log an error and go to the next record). Rather then reporting the error to the error log then going to the next record, the script stops. I need it to keep going through all the records. I tried your suggestions, but no luck. The error is occurring within the For Loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp; except:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("***An error occured processing parcel "+str(value)+". Refer to error log for details.")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "***An error occured processing parcel "+str(value)+". Refer to error log for details."
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; infile.write("An error occured processing parcel "+str(value)+".\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; infile.write(arcpy.GetMessages()+"\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(PointsFL, "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(footprintFL,"CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break
&amp;nbsp;&amp;nbsp;&amp;nbsp; del row
&amp;nbsp;&amp;nbsp;&amp;nbsp; del sc
&amp;nbsp;&amp;nbsp;&amp;nbsp; continue
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:39:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519520#M40661</guid>
      <dc:creator>RachelAlbritton1</dc:creator>
      <dc:date>2021-12-11T22:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519521#M40662</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Which lines is erroring out? If it's within the try method, then I'm really stumped, if it's within the except method or somewhere else in the script, then we'll need to fix it. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In terms of the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;continue&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;break&lt;/SPAN&gt;&lt;SPAN&gt; lines, I think you have them in the wrong spots. That &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;break&lt;/SPAN&gt;&lt;SPAN&gt; line in the except method will break the latest for loop (skipping all records after the first error occurs).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Since the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;continue &lt;/SPAN&gt;&lt;SPAN&gt;is at the very end of the for loop, it's useless. The code would move on to the next record anyway. I wouldn't even use &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;continue&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;break&lt;/SPAN&gt;&lt;SPAN&gt; at this time. My apologizes for being them up.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd suggest trying the script like you have it, but without the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;continue&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;break&lt;/SPAN&gt;&lt;SPAN&gt; lines.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck Rachel. Let me know how it goes!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Jan 2014 13:49:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519521#M40662</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-01-22T13:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519522#M40663</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Get rid of the row and cursor object deletes just before the continue that you added.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The deletes are still inside the for loop&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;destroying the cursor there shoots the for loop in the foot;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;it has nowhere to go.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;conceptually, you have:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. get a row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. try something complex&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. if the try fails, write home then kill the cursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-- and where does the next row for the loop come from???&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;get rid of the break and the continue too, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the loop should continue without prompting if you let it alone.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;break and continue are for when there is further processing inside the loop you want to skip,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;or if you want to jump out of the loop early.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;just try using &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;except: &amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;at least just to try it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This should let the loop run.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Jan 2014 16:57:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519522#M40663</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2014-01-22T16:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Try/Except statement inside of For Loop</title>
      <link>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519523#M40664</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Mdenil - that worked!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Jan 2014 20:17:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/try-except-statement-inside-of-for-loop/m-p/519523#M40664</guid>
      <dc:creator>RachelAlbritton1</dc:creator>
      <dc:date>2014-01-22T20:17:16Z</dc:date>
    </item>
  </channel>
</rss>

