当前位置:   article > 正文

python之异常传递_python 多层except传递 异常向上传递

python 多层except传递 异常向上传递

python之异常传递


  1. try嵌套
try:
    with open("123.txt") as f:
        content = f.read()
        try:
            content[0] = "a"
        except FileNotFoundError:
            print("文件未发现错误")
except BaseException as error:
    print("出现异常:%s" % error)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • try嵌套时, 如果内层try无法捕获该异常,就会向外层try进行传递
  1. 函数嵌套
def func1():
    print("----func1-1----")  
    print(num)
    print("----func1-2----")


def func2():
    try:
        print("----func2-1----")  
        func1()
        print("----func2-2----")

    except:
        print("test2出现异常")  

    print("----func2-3----")  


func2()
print("------华丽的分割线-----")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 函数嵌套时, 如果内层函数无法捕获该异常, 就会向外层函数进行传递
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/631659
推荐阅读
相关标签
  

闽ICP备14008679号