Iva*_*vić 27 python error-handling exception-handling exception
如何处理除一个例外以外的所有例外
try:
something
except <any Exception except for a NoChildException>:
# handling
Run Code Online (Sandbox Code Playgroud)
像这样的东西,除了不破坏原始追溯:
try:
something
except NoChildException:
raise NoChildException
except Exception:
# handling
Run Code Online (Sandbox Code Playgroud)
Gar*_*tty 42
答案是简单地做一个raise
:
try:
...
except NoChildException:
# optionally, do some stuff here and then ...
raise
except Exception:
# handling
Run Code Online (Sandbox Code Playgroud)
这将重新引发最后抛出的异常,原始堆栈跟踪保持不变(即使它已被处理!).