• python 中 try catch finally语句中含有return语句的执行情况


    无论是在try还是在except中,遇到return时,只要设定了finally语句,就会中断当前的return语句,跳转到finally中执行,如果finally中遇到return语句,就直接返回,不再跳转回try/excpet中被中断的return语句

    捕获异常后再次抛出异常:

    捕获后再抛出,会被调用他的上一级再次捕获

     1 def a():
     2     try:
     3         int('N')
     4     except ValueError:
     5         print('a-error')
     6         raise ValueError
     7 
     8 
     9 def b():
    10     try:
    11         a()
    12         print('b-try')
    13     except ValueError:
    14         print('b-error')
    15 
    16 
    17 if __name__ == '__main__':
    18     b()
    19 
    20 # a-error
    21 # b-error
    View Code
     1 def a():
     2     try:
     3         int('N')
     4     except ValueError:
     5         print('a-error')
     6         raise ValueError
     7     except Exception:
     8         print('a-all-error')
     9 
    10 
    11 def b():
    12     try:
    13         a()
    14         print('b-try')
    15     except ValueError:
    16         print('b-error')
    17 
    18 
    19 if __name__ == '__main__':
    20     b()
    21 
    22 # a-error
    23 # b-error
    View Code
    猪猪侠要努力呀!
  • 相关阅读:
    [Leetcode Weekly Contest]285
    [Leetcode Weekly Contest]286
    [Leetcode Weekly Contest]284
    [Leetcode Weekly Contest]287
    阿凡达机器人简介
    PS常用组合键
    PhotoshopCS6 后退多步
    如何卸载Creative Cloud?
    五月实际
    SSO 方案演进
  • 原文地址:https://www.cnblogs.com/mlllily/p/10776004.html
Copyright © 2020-2023  润新知