【发布时间】:2016-02-26 02:39:26
【问题描述】:
我正在使用 execnet 从 python 脚本中调用 jython 模块。
来自docs:
请注意,来自远程执行代码的异常将被重新引发为包含远程回溯的文本表示的 channel.RemoteError 异常。
假设我的远程模块可能导致两个不同的异常,我希望能够以不同的方式处理每个异常。考虑到这两个异常都会抛出一个只包含回溯字符串的RemoteError 异常,我将如何处理这个问题?
例如,这个特定的调用代码:
#...
channel.send('bogus')
导致以下RemoteError,其中只包含一个属性formatted,其中包含一个回溯字符串:
RemoteError: Traceback (most recent call last):
File "<string>", line 1072, in executetask
File "<string>", line 1, in do_exec
File "<remote exec>", line 33, in <module>
IOError: Open failed for table: bogus, error: No such file or directory (2)
我不能做try ... except IOError:。我可以做一个try ... except RemoteError as ex: 并解析ex.formatted 以查看它是否包含IOError,然后改为提出它,但这似乎相当草率:
from execnet.gateway_base import RemoteError
try:
channel.send('bogus')
except RemoteError as ex:
if 'IOError' in ex.formatted:
raise IOError(ex.formatted[ex.formatted.find('IOError'): -1])
if 'ValueError' in ex.formatted:
raise ValueError(ex.formatted[ex.formatted.find('ValueError'): -1])
# otherwise, reraise the uncaptured error:
raise ex
【问题讨论】:
标签: python exception exception-handling