码迷,mamicode.com
首页 > 编程语言 > 详细

Python 中异常嵌套

时间:2019-02-20 21:30:47      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:typeerror   类型   cal   另一个   led   follow   停止   most   file   

在Python中,异常也可以嵌套,当内层代码出现异常时,指定异常类型与实际类型不符时,则向外传,如果与外面的指定类型符合,则异常被处理,直至最外层,运用默认处理方法进行处理,即停止程序,并抛出异常信息。如下代码:

try:
    try:
        raise IndexError
    except TypeError:
        print(get handled)
except SyntaxError:
    print(ok)

运行程序:

Traceback (most recent call last):
  File "<pyshell#47>", line 3, in <module>
    raise IndexError
IndexError

再看另一个被外层try-except捕获的例子:

try:
    try:
        1/0
    finally:
        print(finally)
except:
    print(ok)

运行:

finally
ok

这里值得注意的是except:可以捕获所有的异常,但实际上这样做也有缺点,即有时候会包住预定的异常。

另外,需要提到的是raise A from B,将一个异常与另一个异常关联起来,如果from后面的B没有被外层捕获,那么A,B异常都将抛出,例如:

try:
    1/0
except Exception as E:
    raise TypeError(bad) from E

运行:

Traceback (most recent call last):
  File "<pyshell#4>", line 2, in <module>
    1/0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<pyshell#4>", line 4, in <module>
    raise TypeError(bad) from E
TypeError: bad

相反,如果外层捕获了B:

try:
    try:
        1/0
    except Exception as E:
        raise TypeError from E
except TypeError:
    print(no

运行:

no

最后,再看看try-finally在嵌套中的表现。

 try:
    try:
        1/0
    finally:
        print(finally)
except:
    print(ok)

运行:

finally
ok

不管有没有异常发生,或者其是否被处理,finally的代码都要执行,如果异常被处理,则停止,如果没有被处理,向外走,直至最终没处理,采用默认方法处理,上例中,异常在最外层被处理。

try:
    try:
        1/0
    except Exception as E:
        print(happens)
    finally:
        print(finally)
except E:
    print(get handled)

运行:

happens
finally

异常在内部被处理,不再向外传播。

 

Python 中异常嵌套

标签:typeerror   类型   cal   另一个   led   follow   停止   most   file   

原文地址:https://www.cnblogs.com/johnyang/p/10409153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!