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

 

posted on 2019-04-26 18:44  mlllily  阅读(546)  评论(0编辑  收藏  举报