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 5849287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:58:43+00:00 2026-05-22T12:58:43+00:00

Sometimes I need the following pattern within a for loop. At times more than

  • 0

Sometimes I need the following pattern within a for loop. At times more than once in the same loop:

try:
    # attempt to do something that may diversely fail
except Exception as e:
    logging.error(e)
    continue

Now I don’t see a nice way to wrap this in a function as it can not return continue:

def attempt(x):
    try:
        raise random.choice((ValueError, IndexError, TypeError))
    except Exception as e:
        logging.error(e)
        # continue  # syntax error: continue not properly in loop
        # return continue  # invalid syntax
        return None  # this sort of works

If I return None than I could:

a = attempt('to do something that may diversely fail')
if not a:
    continue

But I don’t feel that does it the justice. I want to tell the for loop to continue (or fake it) from within attempt function.

  • 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-05-22T12:58:44+00:00Added an answer on May 22, 2026 at 12:58 pm

    Python already has a very nice construct for doing just this and it doesn’t use continue:

    for i in range(10):
        try:
            r = 1.0 / (i % 2)
        except Exception, e:
            print(e)
        else:
            print(r)
    

    I wouldn’t nest any more than this, though, or your code will soon get very ugly.

    In your case I would probably do something more like this as it is far easier to unit test the individual functions and flat is better than nested:

    #!/usr/bin/env python
    
    def something_that_may_raise(i):
        return 1.0 / (i % 2)
    
    def handle(e):
        print("Exception: " + str(e))
    
    def do_something_with(result):
        print("No exception: " + str(result))
    
    def wrap_process(i):
        try:
            result = something_that_may_raise(i)
        except ZeroDivisionError, e:
            handle(e)
        except OverflowError, e:
            handle(e) # Realistically, this will be a different handler...
        else:
            do_something_with(result)
    
    for i in range(10):
        wrap_process(i)
    

    Remember to always catch specific exceptions. If you were not expecting a specific exception to be thrown, it is probably not safe to continue with your processing loop.

    Edit following comments:

    If you really don’t want to handle the exceptions, which I still think is a bad idea, then catch all exceptions (except:) and instead of handle(e), just pass. At this point wrap_process() will end, skipping the else:-block where the real work is done, and you’ll go to the next iteration of your for-loop.

    Bear in mind, Errors should never pass silently.

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

Sidebar

Related Questions

Sometimes you need to upgrade the database with many rows that you have in
For diagnostic purposes I sometimes need to store the call stack that lead to
Sometimes I need to perform following command cp -rv demo demo_bkp However I want
Sometimes I need to be sure that some integer is even. As such I
I sometimes need to use Visual Studio when I have limited screen real estate
Sometimes I need to quickly extract some arbitrary data from XML files to put
Sometimes you need to skip execution of part of a method under certain non-critical
I have a ListView which sometimes I need to put around 10000 items in.
I use huge data files, sometimes I only need to know the number of
Sometimes we deploy applications behind customer firewall and we need read only access to

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.