2

假设我有以下 3 个脚本:

script1.py
script2.py
script3.py

可以说我得到这样的回溯:

Traceback (most recent call last):
File "script1.py", line xyz, in ...
...
File "script2.py", line xyz, in ...
...
File "script3.py", line xyz, in ...
...
AttributeError: 'NoneType' object has no attribute 'CoolAttribute'

我在 script3.py 中有几个不同的异常。有没有办法处理 script1.py 中 xyz 行中的任何这些异常,而不处理 script2.py 中的异常?我只想在 script1.py 中的一行中处理源自 script3.py 的异常。

4

1 回答 1

1

一种简单的脏方法是用 try except close 包装你的 simple3.py,并在任何 script3 异常上引发自定义异常。

class BaseSimpleError(Exception):
    """dummy class for all Simple3 errors"""

try:
    ...your simple3.py goes here...
except Exception, e:
    raise BaseSimpleError()

在 script1 中,您应该导入 BaseSimpleError 并在需要时捕获它。

于 2014-08-20T13:26:09.930 回答