finally小知识点

 finally小知识

我们都知道无论try语句中是否抛出异常,finally中的语句一定会被执行。我们来看下面的例子:
try:
   f = open("/tmp/output", "w")
   f.write("hello")
   #raise Exception("something wrong")
finally:
   print("closing file")
   f.close()
代码
不论try中写文件的过程中是否有异常,finally中关闭文件的操作一定会执行。由于finally的这个特性,finally经常被用来做一些清理工作。
我们再来看下面的例子
def func1():
   try:
       return 1
   finally:
       return 2

def func2():
   try:
       raise ValueError()
   except:
       return 1
   finally:
       return 3

print(func1())
print(func2())
例子
这个例子中 func1() 和 func2() 返回什么呢?

答案是 func1() 返回2, func2() 返回3。为什么是这样的呢?我们先来看一段Python官网上对于finally的解释:

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement.

重点部分用粗体标出了,翻成中文就是try块中包含break、continue或者return语句的,在离开try块之前,finally中的语句也会被执行。
所以在上面的例子中,func1() 中,在try块return之前,会执行finally中的语句,try中的return被忽略了,最终返回的值是finally中return的值。func2() 中,try块中抛出异常,被except捕获,在except块return之前,执行finally中的语句,except中的return被忽略,最终返回的值是finally中return的值。
我们在上面的例子中加入print语句,可以更清楚地看到过程
def func1():
   try:
       print 'in func1 try: try statement, will return 1'
       return 1
   finally:
       print 'in func1 finally: try statement, will return 2'
       return 2

def func2():
   try:
       print 'in func2 try: raise error'
       raise ValueError()
   except:
       print 'in func2 except: caught error, will return 1!'
       return 1
   finally:
       print 'in func2 finally: will return 3'
       return 3

print func1()
print func2()
加print代码
上面的代码输出
in func1 try: try statement, will return 1
in func1 finally: try statement, will return 2
2
in func2 try: raise error
in func2 except: caught error, will return 1!
in func2 finally: will return 3
3
结果
我们对上面的func2做一些修改,如下
def func2():
   try:
       print 'in func2 try: raise error'
       raise ValueError()
   except IndexError:
       print 'in func2 except: caught error, will return 1!'
       return 1
   finally:
       print 'in func2 finally: will return 3'
       return 3

print func2()
修改代码
输出如下
in func2 try: raise error
in func2 finally: will return 3
3
结果
try中抛出的异常是ValueError类型的,而except中定位的是IndexError类型的,try中抛出的异常没有被捕获到,所以except中的语句没有被执行,但不论异常有没有被捕获,finally还是会执行,最终函数返回了finally中的返回值3。

这里还可以看到另外一个问题。try中抛出的异常没有被捕获到,按理说当finally执行完毕后,应该被再次抛出,但finally里执行了return,导致异常被丢失。
可以看到在finally中使用return会导致很多问题。实际应用中,不推荐在finally中使用return返回。

猜你喜欢

转载自www.cnblogs.com/songxuexiang/p/9102362.html