Python 如何打印嵌套循环的异常?
我想打印从内部try到外部try捕获的所有异常语句。有没有办法在不更改内部try-catch块的情况下执行此操作Python 如何打印嵌套循环的异常?,python,error-handling,nested-exceptions,Python,Error Handling,Nested Exceptions,我想打印从内部try到外部try捕获的所有异常语句。有没有办法在不更改内部try-catch块的情况下执行此操作 def test_nested_exceptions(): try: try: raise AssertionError('inner error ') except AssertionError as ae: raise AssertionError("error in except
def test_nested_exceptions():
try:
try:
raise AssertionError('inner error ')
except AssertionError as ae:
raise AssertionError("error in except")
finally:
raise AssertionError("error in finally")
except AssertionError as e:
print(e)
您无法访问
finally
块中的错误对象,但可以使用sys
模块获得一些详细信息,如下所示
import sys
def test_nested_exceptions():
try:
try:
raise AssertionError('inner error ')
except AssertionError as ae:
print(ae)
raise AssertionError("error in except")
finally:
print(sys.exc_info())
raise AssertionError("error in finally")
except AssertionError as e:
print(e)
test_nested_exceptions()