Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打印嵌套循环的异常?_Python_Error Handling_Nested Exceptions - Fatal编程技术网

Python 如何打印嵌套循环的异常?

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

我想打印从内部try到外部try捕获的所有异常语句。有没有办法在不更改内部try-catch块的情况下执行此操作

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()