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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:06:20+00:00 2026-06-05T07:06:20+00:00

I got many lines in a row which may throw an exception, but no

  • 0

I got many lines in a row which may throw an exception, but no matter what, it should still continue the next line. How to do this without individually try catching every single statement that may throw an exception?

try:
    this_may_cause_an_exception()
    but_I_still_wanna_run_this()
    and_this()
    and_also_this()
except Exception, e:
    logging.exception('An error maybe occured in one of first occuring functions causing the others not to be executed. Locals: {locals}'.format(locals=locals()))

Let’s see above code, all functions may throw exceptions, but it should still execute the next functions no matter if it threw an exception or not. Is there a nice way of doing that?

I dont wanna do this:

try:
    this_may_cause_an_exception()
except:
    pass
try:
    but_I_still_wanna_run_this()
except:
    pass
try:
    and_this()
except:
    pass
try:
    and_also_this()
except:
    pass

I think code should still continue to run after an exception only if the exception is critical (The computer will burn or the whole system will get messed up, it should stop the whole program, but for many small things also exceptions are thrown such as connection failed etc.)
I normally don’t have any problems with exception handling, but in this case I’m using a 3rd party library which easily throws exceptions for small things.

After looking at m4spy’s answer, i thought wouldn’t it be possible, to have a decorator which will let every line in the function execute even if one of them raises an exception.

Something like this would be cool:

def silent_log_exceptions(func):
    @wraps(func)
    def _wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except Exception:
            logging.exception('...')
            some_special_python_keyword # which causes it to continue executing the next line
    return _wrapper

Or something like this:

def silent_log_exceptions(func):
    @wraps(func)
    def _wrapper(*args, **kwargs):
        for line in func(*args, **kwargs):
            try:
                exec line
            except Exception:
                logging.exception('...')
    return _wrapper



@silent_log_exceptions
def save_tweets():
    a = requests.get('http://twitter.com)
    x = parse(a)
    bla = x * x
  • 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-05T07:06:21+00:00Added an answer on June 5, 2026 at 7:06 am
    for func in [this_may_cause_an_exception,
                 but_I_still_wanna_run_this,
                 and_this,
                 and_also_this]:
        try:
            func()
        except:
            pass
    

    There are two things to notice here:

    • All actions you want to perform have to represented by callables with the same signature (in the example, callables that take no arguments). If they aren’t already, wrap them in small functions, lambda expressions, callable classes, etc.
    • Bare except clauses are a bad idea, but you probably already knew that.

    An alternative approach, that is more flexible, is to use a higher-order function like

    def logging_exceptions(f, *args, **kwargs):
        try:
            f(*args, **kwargs)
        except Exception as e:
            print("Houston, we have a problem: {0}".format(e))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a text file that arrives at my application as many lines of
I have got many errors while installing shotdetect software. some errors were because of
I've got a many to many relationship from Project to Asset using ProjectAsset class
When compiling the following instruction: movl 4(%ebp), 8(%ebp) I got: too many memory reference
After many hair-pulling frustrations I've finally got one version of the PerlMagick module working
In many tutorial of Acl component in cakephp i got instruction that add component
I've got a table containing many rows. The rows are guaranteed to have been
I've got a solution with many projects One of these proejcts is a MyProject.Web
I've got a class with many string arrays. I'd like to have one generic
I've got a ClickOnce application deployed that many machines can install just fine -

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.