Raise an exception from a higher level, a la warnings

后端 未结 3 1886
情书的邮戳
情书的邮戳 2020-12-31 10:56

In the module warnings (https://docs.python.org/3.5/library/warnings.html) there is the ability to raise a warning that appears to come from somewhere earlier in the stack:<

3条回答
  •  执笔经年
    2020-12-31 11:45

    EDIT: The previous version did not provide quotes or explanations.

    I suggest referring to PEP 3134 which states in the Motivation:

    Sometimes it can be useful for an exception handler to intentionally re-raise an exception, either to provide extra information or to translate an exception to another type. The __cause__ attribute provides an explicit way to record the direct cause of an exception.

    When an Exception is raised with a __cause__ attribute the traceback message takes the form of:

    Traceback (most recent call last):
     
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      

    To my understanding this is exactly what you are trying to accomplish; clearly indicate that the reason for the error is not your module but somewhere else. If you are instead trying to omit information to the traceback like your edit suggests then the rest of this answer won't do you any good.


    Just a note on syntax:

    The __cause__ attribute on exception objects is always initialized to None. It is set by a new form of the 'raise' statement:

       raise EXCEPTION from CAUSE
    

    which is equivalent to:

        exc = EXCEPTION
        exc.__cause__ = CAUSE
        raise exc
    

    so the bare minimum example would be something like this:

    def function():
        int("fail")
    
    def check_raise(function):
        try:
            function()
        except Exception as original_error:
            err = RuntimeError("An exception was raised.")
            raise err from original_error
    
    check_raise(function)
    

    which gives an error message like this:

    Traceback (most recent call last):
      File "/PATH/test.py", line 7, in check_raise
        function()
      File "/PATH/test.py", line 3, in function
        int("fail")
    ValueError: invalid literal for int() with base 10: 'fail'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/PATH/test.py", line 12, in 
        check_raise(function)
      File "/PATH/test.py", line 10, in check_raise
        raise err from original_error
    RuntimeError: An exception was raised.
    

    However the first line of the cause is the statement in the try block of check_raise:

      File "/PATH/test.py", line 7, in check_raise
        function()
    

    so before raising err it may (or may not) be desirable to remove the outer most traceback frame from original_error:

    except Exception as original_error:
        err = RuntimeError("An exception was raised.")
        original_error.__traceback__ = original_error.__traceback__.tb_next
        raise err from original_error
    

    This way the only line in the traceback that appears to come from check_raise is the very last raise statement which cannot be omitted with pure python code although depending on how informative the message is you can make it very clear that your module was not the cause of the problem:

    err = RuntimeError("""{0.__qualname__} encountered an error during call to {1.__module__}.{1.__name__}
    the traceback for the error is shown above.""".format(function,check_raise))
    

    The advantage to raising exception like this is that the original Traceback message is not lost when the new error is raised, which means that a very complex series of exceptions can be raised and python will still display all the relevant information correctly:

    def check_raise(function):
        try:
            function()
        except Exception as original_error:
            err = RuntimeError("""{0.__qualname__} encountered an error during call to {1.__module__}.{1.__name__}
    the traceback for the error is shown above.""".format(function,check_raise))
            original_error.__traceback__ = original_error.__traceback__.tb_next
            raise err from original_error
    
    def test_chain():
        check_raise(test)
    
    def test():
        raise ValueError
    
    check_raise(test_chain)
    

    gives me the following error message:

    Traceback (most recent call last):
      File "/Users/Tadhg/Documents/test.py", line 16, in test
        raise ValueError
    ValueError
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/Users/Tadhg/Documents/test.py", line 13, in test_chain
        check_raise(test)
      File "/Users/Tadhg/Documents/test.py", line 10, in check_raise
        raise err from original_error
    RuntimeError: test encountered an error during call to __main__.check_raise
    the traceback for the error is shown above.
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/Users/Tadhg/Documents/test.py", line 18, in 
        check_raise(test_chain)
      File "/Users/Tadhg/Documents/test.py", line 10, in check_raise
        raise err from original_error
    RuntimeError: test_chain encountered an error during call to __main__.check_raise
    the traceback for the error is shown above.
    

    Yes it is long but it is significantly more informative then:

    Traceback (most recent call last):
      File "/Users/Tadhg/Documents/test.py", line 18, in 
        check_raise(test_chain)
    RuntimeError: An exception was raised.
    

    not to mention that the original error is still usable even if the program doesn't end:

    import traceback
    
    def check_raise(function):
        ...
    
    def fail():
        raise ValueError
    
    try:
        check_raise(fail)
    except RuntimeError as e:
        cause = e.__cause__
        print("check_raise failed because of this error:")
        traceback.print_exception(type(cause), cause, cause.__traceback__)
    
    print("and the program continues...")
    

提交回复
热议问题