Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:39:06+00:00 2026-06-12T17:39:06+00:00

I could use some advice on how to handle errors in my Python script.

  • 0

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:

  1. place a polygon around the raster
  2. place a point on the mean center of the polygon
  3. use the point to identify a specific county associated with its
    corresponding raster
  4. 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"

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:39:07+00:00Added an answer on June 12, 2026 at 5:39 pm

    Is this what you need? It will print out the traceback if anything goes wrong with any individual raster in the for loop, and then move on to the next one.

    import traceback
    
    # 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:
        try:
            # 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..."
    
        except:
            print "Something went wrong handling " + str(raster) + ". Here's a traceback:"
            traceback.print_exc()
            continue
    
    print "All processing is now finished"
    

    This is a lazy programmer’s implementation, wrapping the entire contents of the for loop in a try… except… block that will catch exceptions of any type anywhere in the block. Depending upon your needs and tastes, you may find it more helpful (or simply more elegant) to catch only the specific error that you’re getting in practice, as dm03514 suggested – but I figure that for a little script like this whose output is going to be viewed by a human, it doesn’t matter which you do.

    What probably IS important to you, as you specify in the question, is that you see the traceback when an error occurs. That’s where traceback.print_exc() comes in. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an issue i could use some help with, i have python list
I'm planning the structure of a MySql database and could use some advice from
I could use some help with expression conversion. I have a method on a
I'm starting a basic application using Python and PyQt and could use some experienced
I could use some advice/help on a piece of software I've developed. The application
We're having a small issue and could use some help - we have the
I am really new to Ruby and could use some help with a program.
I'm new to SQL and could use some help in creating a database schema
I'm struggling with the following scenario and could use some different perspectives to shed
I'm just starting to learn T-SQL and could use some help in understanding what's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.