python中 try、except、finally 的执行顺序
来源:互联网 发布:票据市场知乎 编辑:程序博客网 时间:2024/06/05 06:10
def test1(): try: print('to do stuff') raise Exception('hehe') print('to return in try') return 'try' except Exception: print('process except') print('to return in except') return 'except' finally: print('to return in finally') return 'finally'test1Return = test1()print('test1Return : ' + test1Return)
输出:
to do stuff
process except
to return in except
to return in finally
test1Return : finally
def test2(): try: print('to do stuff') print('to return in try') return 'try' except Exception: print('process except') print('to return in except') return 'except' finally: print('to return in finally') return 'finally'test2Return = test2()print('test1Return : ' + test2Return)
to do stuff
to return in try
to return in finally
test2Return : finally
这里在 try 中没有抛出异常,因此不会转到 except 中,但是在try 中遇到return时,也会立即强制转到finally中执行,并在finally中返回
test1和test2得到的结论:
无论是在try还是在except中,遇到return时,只要设定了finally语句,就会中断当前的return语句,跳转到finally中执行,如果finally中遇到return语句,就直接返回,不再跳转回try/excpet中被中断的return语句
def test3(): i = 0 try: i += 1 print('i in try : %s'%i) raise Exception('hehe') except Exception: i += 1 print('i in except : %s'%i) return i finally: i += 1 print ('i in finally : %s'%i )print('test3Return : %s'% test3())
输出:
i in try : 1
i in except : 2
i in finally : 3
test3Return : 2
def test4(): i = 0 try: i += 1 return i finally: i += 1 print ('i in finally : %s'%i )print('test4Return : %s' % test4())
i in finally : 2
test4Return : 1
test3和test4得到的结论:
在except和try中遇到return时,会锁定return的值,然后跳转到finally中,如果finally中没有return语句,则finally执行完毕之后仍返回原return点,将之前锁定的值返回(即finally中的动作不影响返回值),如果finally中有return语句,则执行finally中的return语句。
def test5(): for i in range(5): try: print('do stuff %s'%i) raise Exception(i) except Exception: print('exception %s'%i) continue finally: print('do finally %s'%i)test5()
输出
do stuff 0
exception 0
do finally 0
do stuff 1
exception 1
do finally 1
do stuff 2
exception 2
do finally 2
do stuff 3
exception 3
do finally 3
do stuff 4
exception 4
do finally 4
test5得到的结论:
在一个循环中,最终要跳出循环之前,会先转到finally执行,执行完毕之后才开始下一轮循环
- python中 try、except、finally 的执行顺序
- python中 try、except、finally 的执行顺序
- python中 try、except、finally 的执行顺序
- python try except else finally 执行顺序详细分析
- python try except else finally 执行顺序详细分析
- python try except else finally 执行顺序详细分析
- python try except else finally 执行顺序详细分析
- Python中try...except...finally的理解
- try{} finally{} 中 finally的执行顺序
- Python中关于try/except/finally的基础用法
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- python try/except/finally
- Python中try,except,else,finally用法
- vs2013 调试mysql
- [Hive]那些年踩过的Hive坑
- 浅谈CSRF攻击方式
- 记录一些学习angular2中遇到的问题
- Office 2016 for Mac破解版(含破解补丁和教程)
- python中 try、except、finally 的执行顺序
- Android 使用版本控制工具时添加忽略文件方式
- 深度学习中经常看到epoch、 iteration和batchsize
- Java中的集合框架
- C#编写ActiveX控件
- 使用FreeMarker生成静态HTML
- 《门徒》中的,高内聚、低耦合
- 1032. Sharing 解析
- Java - 文档注释(javadoc)