python try except else finally 执行顺序详细分析 - 企业简介 - 正定县枫林商贸有限公司
现在的位置: 主页 > 企业简介 > 文章正文
python try except else finally 执行顺序详细分析
作者:正定县枫林商贸有限公司 来源:www.hbflsm.com 发布时间:2017-09-07 13:21:11
python try except else finally 执行顺序详细分析

学习python或者其他有异常控制的编程语言, 大家很有可能说try except finally(try catch finally)的执行很简单,无非就是有异常的话执行except, finally无论是否有异常都会执行, 大致上原则是这样, 但是如果涉及到更加详细的复杂的路径,加上return 语句,就没有那么简单了。


1. 没有return 语句的情况


print 'this is a test of code path in try...except...else...finally' print '************************************************************' def exceptTest(): try: print 'doing some work, and maybe exception will be raised' raise IndexError('index error') #print 'after exception raise' #return 0 except KeyError, e: print 'in KeyError except' print e #return 1 except IndexError, e: print 'in IndexError except' print e #return 2 except ZeroDivisionError, e: print 'in ZeroDivisionError' print e #return 3 else: print 'no exception' #return 4 finally: print 'in finally' #return 5 resultCode = exceptTest() print resultCode
上面的代码是一直要使用的代码,只不过暂时不用的代码被comment了。

有异常发生,并且捕获异常,最后在finally进行处理,上面代码的输出:

this is a test of code path in try...except...else...finally ************************************************************ doing some work, and maybe exception will be raised in IndexError except index error in finally None
然后我们逐渐给上面代码各个情况添加return 语句, 查看添加return 语句后的代码执行效果。


2. 添加return 语句的情况


print 'this is a test of code path in try...except...else...finally' print '************************************************************' def exceptTest(): try: print 'doing some work, and maybe exception will be raised' raise IndexError('index error') print 'after exception raise' return 0 except KeyError, e: print 'in KeyError except' print e return 1 except IndexError, e: print 'in IndexError except' print e return 2 except ZeroDivisionError, e: print 'in ZeroDivisionError' print e return 3 else: print 'no exception' return 4 finally: print 'in finally' return 5 resultCode = exceptTest() print resultCode
这个时候所有的分支都存在return 语句,并且会引发异常, 看一下输出:

this is a test of code path in try...except...else...finally ************************************************************ doing some work, and maybe exception will be raised in IndexError except index error in finally 5
异常发生后,raise语句以后的不再执行,然后到了捕获异常语句, 但是捕获异常模块有个return , 是不是这个时候就不再继续执行直接返回呢?但是这是跟 finally语句必然执行是相冲突的, 可以在结果中看到finally实际上执行了,并且返回值是5,在 finally de 的返回值。


然后,我们在看看把finally 的返回值注释掉,看看返回值是多少?

代码如下:

print 'this is a test of code path in try...except...else...finally' print '************************************************************' def exceptTest(): try: print 'doing some work, and maybe exception will be raised' raise IndexError('index error') print 'after exception raise' return 0 except KeyError, e: print 'in KeyError except' print e return 1 except IndexError, e: print 'in IndexError except' print e return 2 except ZeroDivisionError, e: print 'in ZeroDivisionError' print e return 3 else: print 'no exception' return 4 finally: print 'in finally' #return 5 resultCode = exceptTest() print resultCode
这个时候的程序输出:

this is a test of code path in try...except...else...finally ************************************************************ doing some work, and maybe exception will be raised in IndexError except index error in finally 2
返回值变为2, 这个时候有点疑惑了, 先不用解释问题,专题,我们继续看其他的情况。


3. 没有异常发生且try语句块没有return

代码如下:

print 'this is a test of code path in try...except...else...finally' print '************************************************************' def exceptTest(): try: print 'doing some work, and maybe exception will be raised' #raise IndexError('index error') print 'after exception raise' #return 0 except KeyError, e: print 'in KeyError except' print e return 1 except IndexError, e: print 'in IndexError except' print e return 2 except ZeroDivisionError, e: print 'in ZeroDivisionError' print e return 3 else: print 'no exception' return 4 finally: print 'in finally' return 5 resultCode = exceptTest() print resultCode
这个时候的代码输出:

this is a test of code path in try...except...else...finally ************************************************************ doing some work, and maybe exception will be raised after exception raise no exception in finally 5
这里验证了如果没有异常那么else语句是执行的,并且finally语句执行,然后返回finally语句的return 5


但是,当try语句块里存在return语句是什么情况呢?

4. 没有异常发生且try语句块 存在return语句

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:黄冈网站建设 http://huanggang.45qun.com


  • 上一篇:Python函数式编程-map()、zip()、filter()、reduce()
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 正定县枫林商贸有限公司 ALL RIGHTS RESERVED.
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100