Adding information to an exception?

前端 未结 9 1783
-上瘾入骨i
-上瘾入骨i 2020-12-02 06:49

I want to achieve something like this:

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

def bar(arg1):
    try:
       foo()
             


        
相关标签:
9条回答
  • 2020-12-02 07:32

    Unlike previous answers, this works in the face of exceptions with really bad __str__. It does modify the type however, in order to factor out unhelpful __str__ implementations.

    I'd still like to find an additional improvement that doesn't modify the type.

    from contextlib import contextmanager
    @contextmanager
    def helpful_info():
        try:
            yield
        except Exception as e:
            class CloneException(Exception): pass
            CloneException.__name__ = type(e).__name__
            CloneException.__module___ = type(e).__module__
            helpful_message = '%s\n\nhelpful info!' % e
            import sys
            raise CloneException, helpful_message, sys.exc_traceback
    
    
    class BadException(Exception):
        def __str__(self):
            return 'wat.'
    
    with helpful_info():
        raise BadException('fooooo')
    

    The original traceback and type (name) are preserved.

    Traceback (most recent call last):
      File "re_raise.py", line 20, in <module>
        raise BadException('fooooo')
      File "/usr/lib64/python2.6/contextlib.py", line 34, in __exit__
        self.gen.throw(type, value, traceback)
      File "re_raise.py", line 5, in helpful_info
        yield
      File "re_raise.py", line 20, in <module>
        raise BadException('fooooo')
    __main__.BadException: wat.
    
    helpful info!
    
    0 讨论(0)
  • 2020-12-02 07:32

    Maybe

    except Exception as e:
        raise IOError(e.message + 'happens at %s'%arg1)
    
    0 讨论(0)
  • 2020-12-02 07:33

    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 讨论(0)
  • 2020-12-02 07:45

    Assuming you don't want to or can't modify foo(), you can do this:

    try:
        raise IOError('stuff')
    except Exception as e:
        if len(e.args) >= 1:
            e.args = (e.args[0] + ' happens',) + e.args[1:]
        raise
    

    This is indeed the only solution here that solves the problem in Python 3 without an ugly and confusing "During handling of the above exception, another exception occurred" message.

    In case the re-raising line should be added to the stack trace, writing raise e instead of raise will do the trick.

    0 讨论(0)
  • 2020-12-02 07:47

    One handy approach that I used is to use class attribute as storage for details, as class attribute is accessible both from class object and class instance:

    class CustomError(Exception):
        def __init__(self, details: Dict):
            self.details = details
    

    Then in your code:

    raise CustomError({'data': 5})
    

    And when catching an error:

    except CustomError as e:
        # Do whatever you want with the exception instance
        print(e.details)
    
    0 讨论(0)
  • 2020-12-02 07:50

    You can define your own exception that inherits from another and create it's own constructor to set value.

    For example:

    class MyError(Exception):
       def __init__(self, value):
         self.value = value
         Exception.__init__(self)
    
       def __str__(self):
         return repr(self.value)
    
    0 讨论(0)
提交回复
热议问题