捕获2个异常并使用“ as”无法正常工作

时间:2019-06-11 10:43:41

标签: python exception raise

我正在尝试捕获以下两个异常:

class TestException:
    def __init__(self):
        self.x = [1, 2, 3]

def main():
    test_exception = TestException()
    try:
        test_exception.y[1] = 4.0
    except (IndexError, AttributeError) as e:
        raise e('Why does this not work?')

if __name__ == '__main__':
    main()

但出现以下错误:

TypeError:“ AttributeError”对象不可调用

为什么会这样?因为以下方法可以正常工作:

raise AttributeError('This does work!')

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

从@jonrsharpe开始,以下工作正常:

[SEVERE] built_value_generator:built_value on lib/client.dart (cached):
Unknown error in BuiltValueGenerator for myproject|lib/client.dart.
NoSuchMethodError: The method 'getParsedLibraryByElement' was called on null.
Receiver: null
Tried calling: getParsedLibraryByElement(Instance of 'LibraryElementImpl')

raise type(e)('This works now.')