zoukankan      html  css  js  c++  java
  • Python 中异常嵌套

    在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

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

    ##### 愿你一寸一寸地攻城略地,一点一点地焕然一新 #####
  • 相关阅读:
    JS reduce方法的使用
    面试娱录
    sticky置顶功能影响了锚点定位
    postcss-px-to-viewport移动端自适应
    axios请求参数自动拼接到了地址那里
    ping 不通。无法访问目标主机
    JS前后台方法的相互调用
    SQL server2008 无法连接服务器
    Assembly.Load未能加载文件或程序集“”或它的某一个依赖项。系统找不到指定的文件
    JS判断IE和非IE
  • 原文地址:https://www.cnblogs.com/johnyang/p/10409153.html
Copyright © 2011-2022 走看看