Pythonic方法从模块中捕获所有异常?

时间:2015-09-12 08:53:58

标签: python exception exception-handling rope

我正在尝试使用rope包进行一些重构。根据代码,它可能会抛出异常,并且有超过10个例外情况。

我不想做

from rope.base.exceptions import *

try:
    # do something
except (AttributeNotFoundError, ModuleDecodeError,
        ..., ..., ..., RefactoringError) as e:
     # do something else

我只想抓住所有绳索异常,就像这样

import rope

try:
    # do something
except rope.base.exceptions.*:
    # do something else

如何捕获特定模块的所有异常?

1 个答案:

答案 0 :(得分:4)

抓住所有例外的基础:

In [5]: import rope.base.exceptions as rbe
In [6]: try:
   ...:     raise rbe.AttributeNotFoundError
   ...: except rbe.RopeError, e:
   ...:     print "RopeError -", e
   ...:

RopeError!