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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:48:17+00:00 2026-06-11T03:48:17+00:00

I often find myself wanting to do something like this, I have something wrapped

  • 0

I often find myself wanting to do something like this, I have something wrapped in try excepts like this

item= get_item()
try:
    do_work(item)
except SomeError as err:
    if err.code == 123:
        do_something(item)
    else:
        # Actually I don't want to do something with this error code... I want to handle in 'except'
except:
    put_back(item)
    raise

Is there a way to raise into the except block below from the else? (a continue would be nice) I end up doing something like the following which isn’t as clean

item= get_item()
try:
    try:
        do_work(item)
    except SomeError as err:
        if err.code == 123:
            do_something(item)
        else:
            raise
 except:
     put_back(item)
     raise

Is there anyway to do that?

  • 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-11T03:48:19+00:00Added an answer on June 11, 2026 at 3:48 am

    If you are using a recent enough python version (2.5 and up), you should switch to using a context manager instead:

    class WorkItemContextManager(object):
        def __enter__(self):
            self.item = get_item()
            return self.item
    
        def __exit__(self, exc_type, exc_value, tb):
            if exc_type is not None:
                if exc_type is SomeError and exc_value.code == 123:
                    do_something(self.item)
                    return True  # Exception handled
                put_back(self.item)
    

    Then:

    with WorkItemContextManager() as item:
        do_work(item)
    

    The __exit__ method can return True if an exception has been handled; returning None will instead re-raise any exceptions raised in the with block.

    If not, you are looking for a finally block instead:

    item = get_item()
    try:
        do_work(item)
        item = None
    except SomeError as err:
        if err.code == 123:
            do_something(item)
            item = None
    finally:
        if item is not None:
            put_back(item)
    

    The finally suite is guaranteed to be executed when the try: suite completes, or an exception has occurred. By setting item to None you basically tell the finally suite everything completed just fine, no need to put it back.

    The finally handler takes over from your blanket except handler. If there has been an exception in do_work, item will not be set to None. If the SomeError handler doesn’t catch the exception, or err.code is not 123, item will also not be set to None, and thus the put_back(item) method is executed.

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

Sidebar

Related Questions

I find myself often wanting to write Python list comprehensions like this: nearbyPoints =
I often find myself doing something like this a lot: something | grep cat
I often find myself executing commands like this at bash : history | grep
This is a very simple question. I often find myself wanting to create a
I often find myself wanting to insert regular functions into a binded sequence. Like
I often find myself doing things like this when manipulating tables:- $($('table tr').children()[2]).html(); For
I often find myself wanting to change just something little in a colorscheme, but
I often find myself wanting to copy the contents of arrays that have a
I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
I often find myself wanting to write an SQL query like the following: SELECT

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.