最近,我遇到了一个我从未见过的代码示例:
try:
# a simple bunch of code
if sample == 0:
return True
else:
raise ExampleError()
except not ExampleError:
raise AnotherExampleError()它是如何工作的(如果工作的话)?
发布于 2019-05-10 13:09:57
据我所知,这在Python的任何版本上都不会成功。因为not操作符总是产生一个布尔值(True或False),所以这里试图捕捉其中的一个值,在这里是False。因为您不能抛出True或False,所以这是没有用的。
我认为作者的意图是这样的:
try:
raise ExampleError()
except ExampleError e:
throw e
except:
raise AnotherExampleError()https://stackoverflow.com/questions/56078086
复制相似问题