你的观察不准确;必须有 2 个地方 raise some_exeption
,或者你明确地重新加注它。
一旦一个异常被正确捕获,它就不会被堆栈中更高的其他处理程序捕获。
这是最简单的演示:
>>> try:
... try:
... raise AttributeError('foo')
... except AttributeError as e:
... print 'caught', e
... except AttributeError as e:
... print 'caught the same exception again?', e
...
caught foo
仅调用了内部处理程序。 except
另一方面,如果我们重新引发异常,您将看到两个处理程序都打印一条消息:
>>> try:
... try:
... raise AttributeError('foo')
... except AttributeError as e:
... print 'caught', e
... raise e
... except AttributeError as e:
... print 'caught the same exception again?', e
...
caught foo
caught the same exception again? foo
As such, there is no need to have to handle 'exceptions that are not caught by the function'; you will only need to handle exceptions that were not already caught earlier.