在 Python 中的异常处理程序中引发异常的惯用方法是什么?
what is the idiomatic way to raise an exception within the exception handler in Python?
Python 中捕获异常并引发另一个异常的正确方法是什么?即,我想捕获类型 1 的异常,操作 Exception 对象并无条件地引发类型 2 的第二个异常,以便调用函数看不到类型 1 的异常但绝对看到类型 2 的异常,其构造取决于从类型 1 异常访问的数据。
这是我试过但不起作用的代码。
def d(wrt):
try:
return rt.derivative(wrt).canonicalize()
except CannotComputeDerivative as e:
msg = "\n".join([f"When generating derivatives from {self}",
f" when computing edges of {rt}",
f" which canonicalizes to {self.canonicalize()}",
f" computing derivative of {e.rte}",
f" wrt={e.wrt}",
f" derivatives() reported: {e.msg}"])
raise CannotComputeDerivatives(msg=msg,
rte=rt,
wrt=wrt,
first_types=fts,
mdtd=wrts)
我认为它不起作用的原因是我收到以下消息作为输出:
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 95, in d
return rt.derivative(wrt).canonicalize()
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_singleton.py", line 68, in derivative
return super().derivative(wrt)
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 72, in derivative
return self.derivative_down(wrt)
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_singleton.py", line 91, in derivative_down
raise CannotComputeDerivative(
rte.r_rte.CannotComputeDerivative
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
self.assertTrue(rt.derivatives())
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives
根据 MisterMiyagi 的建议,我在 raise CannotComputeDerivatives(...)
之后添加了 from None
。这取得了进展,但似乎 unittest
环境不太喜欢它。单元测试期间显示的消息如下所示。看起来 unittest
遗憾地截断了消息。
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
rt.derivatives()
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives
=============
跟进解决方案:
最初的问题是 CannotComputeDerivatives
class 的 __init__
函数在调用 super().__init__()
时没有传递消息字符串。我已将 class 定义更新如下。
class CannotComputeDerivatives(Exception):
def __init__(self, msg, rte, wrt, first_types, mdtd):
self.msg = msg
self.rte = rte
self.wrt = wrt
self.first_types = first_types
self.mdtd = mdtd
super().__init__(msg)
结果是我在单元测试期间收到漂亮的错误消息:
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
self.assertTrue(rt.derivatives())
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives: When generating derivatives from Singleton(SOr(odd?, SAtomic(Test2)))
when computing edges of Singleton(SOr(odd?, SAtomic(Test2)))
which canonicalizes to Singleton(SOr(odd?, SAtomic(Test2)))
computing derivative of Singleton(SOr(odd?, SAtomic(Test2)))
wrt=SOr(SAtomic(Test2), odd?)
derivatives() reported: Singleton.derivative_down cannot compute derivative of Singleton(SOr(odd?, SAtomic(Test2)))
wrt=SOr(SAtomic(Test2), odd?)
disjoint=False
subtypep=None
感谢@MisterMiyagi 建议在 raise ...
之后添加 from None
。这样就避免了系统报异常中异常的问题。
Python 中捕获异常并引发另一个异常的正确方法是什么?即,我想捕获类型 1 的异常,操作 Exception 对象并无条件地引发类型 2 的第二个异常,以便调用函数看不到类型 1 的异常但绝对看到类型 2 的异常,其构造取决于从类型 1 异常访问的数据。
这是我试过但不起作用的代码。
def d(wrt):
try:
return rt.derivative(wrt).canonicalize()
except CannotComputeDerivative as e:
msg = "\n".join([f"When generating derivatives from {self}",
f" when computing edges of {rt}",
f" which canonicalizes to {self.canonicalize()}",
f" computing derivative of {e.rte}",
f" wrt={e.wrt}",
f" derivatives() reported: {e.msg}"])
raise CannotComputeDerivatives(msg=msg,
rte=rt,
wrt=wrt,
first_types=fts,
mdtd=wrts)
我认为它不起作用的原因是我收到以下消息作为输出:
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 95, in d
return rt.derivative(wrt).canonicalize()
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_singleton.py", line 68, in derivative
return super().derivative(wrt)
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 72, in derivative
return self.derivative_down(wrt)
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_singleton.py", line 91, in derivative_down
raise CannotComputeDerivative(
rte.r_rte.CannotComputeDerivative
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
self.assertTrue(rt.derivatives())
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives
根据 MisterMiyagi 的建议,我在 raise CannotComputeDerivatives(...)
之后添加了 from None
。这取得了进展,但似乎 unittest
环境不太喜欢它。单元测试期间显示的消息如下所示。看起来 unittest
遗憾地截断了消息。
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
rt.derivatives()
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives
=============
跟进解决方案:
最初的问题是 CannotComputeDerivatives
class 的 __init__
函数在调用 super().__init__()
时没有传递消息字符串。我已将 class 定义更新如下。
class CannotComputeDerivatives(Exception):
def __init__(self, msg, rte, wrt, first_types, mdtd):
self.msg = msg
self.rte = rte
self.wrt = wrt
self.first_types = first_types
self.mdtd = mdtd
super().__init__(msg)
结果是我在单元测试期间收到漂亮的错误消息:
Error
Traceback (most recent call last):
File "/Users/jnewton/Repos/python-rte/pyrte/tests/rte_tests.py", line 615, in test_derivatives
self.assertTrue(rt.derivatives())
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 111, in derivatives
return trace_graph(self, edges)
File "/Users/jnewton/Repos/python-rte/pyrte/genus/utils.py", line 199, in trace_graph
es = edges(v0) # List[(L,V)]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in edges
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 109, in <listcomp>
return [(td, d(td)) for td in wrts]
File "/Users/jnewton/Repos/python-rte/pyrte/rte/r_rte.py", line 103, in d
raise CannotComputeDerivatives(msg=msg,
rte.r_rte.CannotComputeDerivatives: When generating derivatives from Singleton(SOr(odd?, SAtomic(Test2)))
when computing edges of Singleton(SOr(odd?, SAtomic(Test2)))
which canonicalizes to Singleton(SOr(odd?, SAtomic(Test2)))
computing derivative of Singleton(SOr(odd?, SAtomic(Test2)))
wrt=SOr(SAtomic(Test2), odd?)
derivatives() reported: Singleton.derivative_down cannot compute derivative of Singleton(SOr(odd?, SAtomic(Test2)))
wrt=SOr(SAtomic(Test2), odd?)
disjoint=False
subtypep=None
感谢@MisterMiyagi 建议在 raise ...
之后添加 from None
。这样就避免了系统报异常中异常的问题。