相关疑难解决方法(0)

当我在响应中提出自己的异常时,如何更容易地抑制先前的异常?

考虑

try:
   import someProprietaryModule
except ImportError:
   raise ImportError('It appears that <someProprietaryModule> is not installed...')
Run Code Online (Sandbox Code Playgroud)

运行时,如果未安装someProprietaryModule,则可以看到:

(traceback data)
ImportError: unknown module: someProprietaryModule

During handling of the above exception, another exception occurred:

(traceback data)
ImportError: It appears that <someProprietaryModule> is not installed...
Run Code Online (Sandbox Code Playgroud)

也许我不希望出现"在处理上述异常......"行(以及它上面的行).我能做到这一点:

_moduleInstalled = True
try:
   import someProprietaryModule
except ImportError:
   _moduleInstalled = False
if not _moduleInstalled: 
   raise ImportError('It appears that <someProprietaryModule> is not installed...')
Run Code Online (Sandbox Code Playgroud)

但这感觉有点像黑客.我还能做什么?

python exception traceback python-3.x python-3.3

24
推荐指数
1
解决办法
4439
查看次数

标签 统计

exception ×1

python ×1

python-3.3 ×1

python-3.x ×1

traceback ×1