python - 在 Python 中使用自定义异常动态扩展异常?

标签 python python-3.x exception

假设我有一个特殊的异常(exception),它做了一些整洁而美妙的事情 - 解决世界饥饿、对人类的善意、伐木等:

class SpecialException(Exception):
    # Does something really neat here.
    pass
现在假设可能会引发异常,但我们不知道会遇到什么类型的异常:
def crashAndBurn():
    try:
        import random
        choice = random.choice([1,2])
        if choice == 1:
            int('asdf') # Can't cast string to int: ValueError.
        else:
            x # Variable `x` doesn't exist: NameError.
    except Exception as e:
        # Code to wrap `e` into `SpecialException` class
        raise e
当引发未知类型的异常时,我们想要捕获它,将它包装在我们的 SpecialException 中。类,并提升它以便它可以被抛出的原始类型的异常捕获,或者通过捕获 SpecialException :
try:
    crashAndBurn()
except ValueError as e:
    print('This time it was a ValueError, and we want to handle this differently for some reason')
except SpecialException as e:
    print('Handle this more generically')

关于如何(合理)解决这个问题的任何建议?
总之,我们需要:
  • 能够捕获最初引发的异常,并能够捕获 SpecialException
  • 保留回溯以便于调试
  • 包含原始异常的错误消息

  • 我试过的:
    尝试使用 raise SpecialException from e .虽然我们能够从最初引发的异常中查看错误消息和回溯,但我们不再能够通过最初抛出的异常类型来捕获它... 例如:我们可以捕获 SpecialException ,但抓不到ValueError :
    def crashAndBurn():
        try:
            int('asdf') # ValueError
        except Exception as e:
            raise SpecialException('Special Exception Encountered').with_traceback(e.__traceback__) from e
    
    try:
        crashAndBurn()
    except ValueError as e:
        print('This will never be encountered')
    except SpecialException as e:
        print('This will be encountered, when we wanted it to be handled above')
    
    然而,我们在技术上获得的最接近的满足了我们的需求:
  • 虽然引发的异常可以被捕获为 SpecialExceptionValueError ,它实际上是作为另一个一次性使用类提出的:DynamicSpecialException
  • 这真的很恶心,而且看起来非常不 Pythonic
  • def crashAndBurn():
        try:
            int('asdf') # ValueError
        except Exception as e:
            class DynamicSpecialException(SpecialException, e.__class__):
                pass # I feel dirty
            raise DynamicSpecialException('Special Exception Encountered').with_traceback(e.__traceback__)
    
    try:
        crashAndBurn()
    except ValueError as e:
        print('Caught as a ValueError!')
    
    try:
        crashAndBurn()
    except SpecialException as e:
        print('Caught as SpecialException!')
    
    我真正期待找到的是类似于 raise e.extend(SpecialException) 的东西。或 raise SpecialException.from(e) - 而不是这个兔子洞,我今天似乎摆动了下来! :)

    最佳答案

    这里有点小题大做。它似乎可以完成您想要的大部分工作,只是它附加了特殊工厂处理的堆栈跟踪。
    我学到的是你不能交换异常类,e.__class__ = <dynamic class> ,您必须创建一个新的并提高它。

    import pdb
    from traceback import print_exc as xp
    import sys
    
    def cpdb():
        """ put `pdb` on commmand line to halt execution in debugger """
        return "pdb" in sys.argv
    
    
    
    class SpecialException(Exception):
    
        def solve_world_hunger(self):
            print(f"eat more 🦄")
    
    def specialfactory(e):
        """ creates a class dynamically and keeps the original as a base"""
        cls_ = type("SpecialException", (SpecialException, e.__class__),{})
        e2 = cls_(str(e))
        e2.ori = e
        e2.__dict__.update(**e.__dict__)
    
        # 👇 you can try different flavors to see what changes:
        #  basically, I haven't found a way to suppress `The above exception was the direct cause of the following exception:`
        #  see also https://stackoverflow.com/questions/33809864/disable-exception-chaining-in-python-3
    
        # return e2
        # raise e2.        raise specialfactory(e).with_traceback(e.__traceback__) from e
        # raise e2 from e
        raise e2.with_traceback(e.__traceback__) from e
    
    
    def crashAndBurn(no_special=False, custom_message=None):
        try:
    
            if custom_message:
                exc = ValueError(custom_message)
                exc.foo = "bar"
                raise exc
    
            int('asdf') # ValueError
        except Exception as e:
            if no_special:
                #just to investigate what things look like for a plain ValueError
                raise
            # raise specialfactory(e).with_traceback(e.__traceback__) from e
            raise specialfactory(e) from e
    
    
    
    #################################################################
    # check what a regular unchanged ValueError looks like
    #################################################################
    
    try:
        print("\n\n\n🔬regular ValueError, unchanged")
        crashAndBurn(no_special=1)
    except ValueError as e:
        if cpdb(): pdb.set_trace()
        print(f'  plain ValueError: {e}')
        xp()
    except SpecialException as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a SpecialException!: {e}')
        xp()
    
    #################################################################
    # catch a Special as a ValueError
    #################################################################
    
    
    try:
        print("\n\n\n🔬ValueError ")
        crashAndBurn()
    except ValueError as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a ValueError! {e}')
        xp()
    except SpecialException as e:
        if cpdb(): pdb.set_trace()
        print(f' Caught as a SpecialException! {e}')
        xp()
    
    #################################################################
    # catch a Special 
    #################################################################
    
    
    
    try:
        print("\n\n\n🔬SpecialException handling")
        crashAndBurn()
    except SpecialException as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a SpecialException! {e} {e.solve_world_hunger()}')
        xp()
    except ValueError as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a ValueError! {e}')
        xp()
    
    #################################################################
    # custom variables are still available
    #################################################################
    
    try:
        print("\n\n\n🔬ValueError with custom_message/content ")
        crashAndBurn(custom_message="my custom_message")
    except SpecialException as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a SpecialException! {e} {e.foo=} {e.solve_world_hunger()}')
        xp()
    except ValueError as e:
        if cpdb(): pdb.set_trace()
        print(f'  Caught as a ValueError! {e}')
        xp()
    
    
    输出:
    Traceback (most recent call last):
      File "test_183.py", line 57, in <module>
        crashAndBurn(no_special=1)
      File "test_183.py", line 41, in crashAndBurn
        int('asdf') # ValueError
    ValueError: invalid literal for int() with base 10: 'asdf'
    Traceback (most recent call last):
      File "test_183.py", line 41, in crashAndBurn
        int('asdf') # ValueError
    ValueError: invalid literal for int() with base 10: 'asdf'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "test_183.py", line 74, in <module>
        crashAndBurn()
      File "test_183.py", line 47, in crashAndBurn
        raise specialfactory(e) from e
      File "test_183.py", line 30, in specialfactory
        raise e2.with_traceback(e.__traceback__) from e
      File "test_183.py", line 41, in crashAndBurn
        int('asdf') # ValueError
    SpecialException: invalid literal for int() with base 10: 'asdf'
    Traceback (most recent call last):
      File "test_183.py", line 41, in crashAndBurn
        int('asdf') # ValueError
    ValueError: invalid literal for int() with base 10: 'asdf'
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "test_183.py", line 92, in <module>
        crashAndBurn()
      File "test_183.py", line 47, in crashAndBurn
        raise specialfactory(e) from e
      File "test_183.py", line 30, in specialfactory
        raise e2.with_traceback(e.__traceback__) from e
      File "test_183.py", line 41, in crashAndBurn
        int('asdf') # ValueError
    SpecialException: invalid literal for int() with base 10: 'asdf'
    Traceback (most recent call last):
      File "test_183.py", line 39, in crashAndBurn
        raise exc
    ValueError: my custom_message
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "test_183.py", line 108, in <module>
        crashAndBurn(custom_message="my custom_message")
      File "test_183.py", line 47, in crashAndBurn
        raise specialfactory(e) from e
      File "test_183.py", line 30, in specialfactory
        raise e2.with_traceback(e.__traceback__) from e
      File "test_183.py", line 39, in crashAndBurn
        raise exc
    SpecialException: my custom_message
    
    
    
    🔬regular ValueError, unchanged
      plain ValueError: invalid literal for int() with base 10: 'asdf'
    
    
    
    🔬ValueError 
      Caught as a ValueError! invalid literal for int() with base 10: 'asdf'
    
    
    
    🔬SpecialException handling
    eat more 🦄
      Caught as a SpecialException! invalid literal for int() with base 10: 'asdf' None
    
    
    
    🔬ValueError with custom_message/content 
    eat more 🦄
      Caught as a SpecialException! my custom_message e.foo='bar' None
    

    关于python - 在 Python 中使用自定义异常动态扩展异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64218275/

    相关文章:

    c++ - 如何捕获 C++ 异常?

    php - 在 PHP 中,什么是 `ClosedGeneratorException` ?

    python - 使用 AWS 作为 MQTT 代理将 Raspberry Pi #1 链接到 Raspberry Pi #2

    python - 查看Docker Swarm命令行输出

    python - pyinstaller编译的文件有ssl问题,错误: 185090050

    python - CSV数据通过Python上传到SQL表时出现错误

    python - 在 Debian Wheezy 上使用 flask-sqlalchemy 怪异的内存使用和泄漏

    python-3.x - 在 fastai 库中使用 download_data() 和 untar_data()

    python - 雅虎天气 API 2019 - 类型错误/属性错误

    java - Java 语言在哪里定义 Throwable 是否被检查?