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

  • Home
  • SEARCH
  • 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 5943445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:23:47+00:00 2026-05-22T16:23:47+00:00

I want to achieve something like this: def foo(): try: raise IOError(‘Stuff ‘) except:

  • 0

I want to achieve something like this:

def foo():
   try:
       raise IOError('Stuff ')
   except:
       raise

def bar(arg1):
    try:
       foo()
    except Exception as e:
       e.message = e.message + 'happens at %s' % arg1
       raise

bar('arg1')
Traceback...
  IOError('Stuff Happens at arg1')

But what I get is:

Traceback..
  IOError('Stuff')

Any clues as to how to achieve this? How to do it both in Python 2 and 3?

  • 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-22T16:23:48+00:00Added an answer on May 22, 2026 at 4:23 pm

    I’d do it like this so changing its type in foo() won’t require also changing it in bar().

    def foo():
        try:
            raise IOError('Stuff')
        except:
            raise
    
    def bar(arg1):
        try:
            foo()
        except Exception as e:
            raise type(e)(e.message + ' happens at %s' % arg1)
    
    bar('arg1')
    

    Traceback (most recent call last):
      File "test.py", line 13, in <module>
        bar('arg1')
      File "test.py", line 11, in bar
        raise type(e)(e.message + ' happens at %s' % arg1)
    IOError: Stuff happens at arg1
    

    Update 1

    Here’s a slight modification that preserves the original traceback:

    ...
    def bar(arg1):
        try:
            foo()
        except Exception as e:
            import sys
            raise type(e), type(e)(e.message +
                                   ' happens at %s' % arg1), sys.exc_info()[2]
    
    bar('arg1')
    

    Traceback (most recent call last):
      File "test.py", line 16, in <module>
        bar('arg1')
      File "test.py", line 11, in bar
        foo()
      File "test.py", line 5, in foo
        raise IOError('Stuff')
    IOError: Stuff happens at arg1
    

    Update 2

    For Python 3.x, the code in my first update is syntactically incorrect plus the idea of having a message attribute on BaseException was retracted in a change to PEP 352 on 2012-05-16 (my first update was posted on 2012-03-12). So currently, in Python 3.5.2 anyway, you’d need to do something along these lines to preserve the traceback and not hardcode the type of exception in function bar(). Also note that there will be the line:

    During handling of the above exception, another exception occurred:
    

    in the traceback messages displayed.

    # for Python 3.x
    ...
    def bar(arg1):
        try:
            foo()
        except Exception as e:
            import sys
            raise type(e)(str(e) +
                          ' happens at %s' % arg1).with_traceback(sys.exc_info()[2])
    
    bar('arg1')
    

    Update 3

    A commenter asked if there was a way that would work in both Python 2 and 3. Although the answer might seem to be “No” due to the syntax differences, there is a way around that by using a helper function like reraise() in the six add-on module. So, if you’d rather not use the library for some reason, below is a simplified standalone version.

    Note too, that since the exception is reraised within the reraise() function, that will appear in whatever traceback is raised, but the final result is what you want.

    import sys
    
    if sys.version_info.major < 3:  # Python 2?
        # Using exec avoids a SyntaxError in Python 3.
        exec("""def reraise(exc_type, exc_value, exc_traceback=None):
                    raise exc_type, exc_value, exc_traceback""")
    else:
        def reraise(exc_type, exc_value, exc_traceback=None):
            if exc_value is None:
                exc_value = exc_type()
            if exc_value.__traceback__ is not exc_traceback:
                raise exc_value.with_traceback(exc_traceback)
            raise exc_value
    
    def foo():
        try:
            raise IOError('Stuff')
        except:
            raise
    
    def bar(arg1):
        try:
           foo()
        except Exception as e:
            reraise(type(e), type(e)(str(e) +
                                     ' happens at %s' % arg1), sys.exc_info()[2])
    
    bar('arg1')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to achieve something like this in C# 3.5: public void Register<T>() :
Essentially I want to do something like this: class foo: x = 4 @property
I want to achieve something like this in Enterprise Architect's Communication Diagrams: start() ----------------
I'm using WPF 3.5 SP1 and I want to achieve something like this (the
I want to achieve something like this: A) Is an square image, say 65x65.
I want to achieve something like this: BEFORE: SOME_TEXT=some_more_text OTHER_TEXT=some_other_text AFTER: SOME_TEXT OTHER_TEXT I
I want to achieve something like this. http://jsfiddle.net/dpnay/ . A textarea with some text
I want to achieve something like the following: UrlBuilder ub = new UrlBuilder(http://www.google.com/search); ub.Parameters.Add(q,request);
In .NET you can achieve something like this: Color yellowColor = Color.FromName(yellow); Is there
I need to achieve something like this for my website: Flash Rain Effect Is

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.