美文网首页
Python的异常处理(二)

Python的异常处理(二)

作者: 上发条的树 | 来源:发表于2016-05-26 17:18 被阅读674次

    处理多个异常

    处理多个异常,并不是同时报出多个异常。程序运行时,只要遇到一个异常,就会有所反应。所以,每次捕获到的异常,一定是一个。所以,所谓的处理多个异常,意思是说容许捕获不同的异常,在不同的except子句中处理。
    形式如下:

    try:
            print "try子句"
        except xxx:
            print "异常xxx"
        except yyy:
            print "异常yyy"
    

    例子:

    #!usr/bin/env Python
    # coding=utf-8
    while 1:
        print "this is a division program."
        c = raw_input("input 'c' continue,otherwise logot:")
        if c == 'c':
            a = raw_input("first number:")
            b = raw_input("second number:")
            try:
                print float(a)/float(b)
            except ZeroDivisionError:
                print "the second number can't be zero"
                print "************************"
            except ValueError:
                print "please input number"
                print "************************"
        else:
            break 
    

    与上一篇的例子相比,多了一个异常捕获。当输入的分母不是数字时,可以捕获该异常。测试如下:

    $ python tryExcept1.py
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:3
    second number:a
    please input number
    ************************
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:3
    second number:0
    the second number can't be zero
    ************************
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:5
    second number:2
    2.5
    this is a division program.
    input 'c' continue,otherwise logot:p
    $
    

    除了使用多个except之外,还可以在一个except后面放多个异常参数,可以将except部分修改为如下:

    except (ZeroDivisionError,ValueError):
        print "please input rightly"
        print "*********************"
    

    运行结果:

    $ python tryExcept1.py
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:2
    second number:a
    please input rightly
    *********************
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:3
    second number:0
    please input rightly
    *********************
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:4
    second number:2
    2.0
    this is a division program.
    input 'c' continue,otherwise logot:d
    mobao:PythonExample mobao$ 
    

    特别注意,一个except,多个异常参数时,异常参数需要放进()中。

    如果需要呈现内置的异常信息,则可做如下更改:

    except (ZeroDivisionError,ValueError), e:
        print e
        print "*********************"
    

    在Python3中,则如下:

    except (ZeroDivisionError,ValueError) as e:
        print e
        print "*********************"
    

    运行效果:

    python tryExcept1.py
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:2
    second number:a
    could not convert string to float: a
    *********************
    this is a division program.
    input 'c' continue,otherwise logot:c
    first number:2
    second number:0
    float division by zero
    *********************
    this is a division program.
    

    "could not convert string to float: a"和"float division by zero"皆为内置异常信息。

    以上的写法,处理了两个异常,如果是不止两个异常呢?可以这样:execpt: 或者 except Exception, e,后面什么参数也不写就好了。

    else子句

    一般try...except...,在一般情况下是够用的。但是总有不一般的时候出现,所以增加了一个else子句。如:

    >>> try:
    ...     print "I am try"
    ... except:
    ...     print "I am except"
    ... else:
    ...     print "I am else"
    ... 
    I am try
    I am else
    

    可以看出,如果执行了try,则except()子句会被直接忽略,else子句会被执行。

    >>> try:
    ...     print 1/0
    ... except:
    ...     print "I am except"
    ... else:
    ...     print "I am else"
    ... 
    I am except
    

    如果执行了except子句,则不执行else中的文件。
    例如:

    #!usr/bin/env Python
    # coding=utf-8
    while 1:
        try:
            a = raw_input("first number:")
            b = raw_input("second number:")
            print float(a)/float(b)
        except Exception, e:
            print e
            print "try again"
        else:
            break 
    

    结果如下:

    $ python tryExcept1.py
    first number:2
    second number:a
    could not convert string to float: a
    try again
    first number:3
    second number:0
    float division by zero
    try again
    first number:5
    second number:2
    2.5
    $
    

    finally

    无论try子句执行还是except子句执行,finally中一定执行。

    >>> x = 10
    >>> try:
    ...     x = 2/1
    ... except Exception,e:
    ...     print e
    ... finally:
    ...     print "del x"
    ...     del x
    ... 
    del x
    
    >>> x = 10
    >>> try:
    ...     x = 1/0
    ... except Exception,e:
    ...     print e
    ... finally:
    ...     print "del x"
    ...     del x
    ... 
    integer division or modulo by zero
    del x
    

    当然,在应用中,可以将上面的各个子句都综合起来使用,写成如下样式:

    try:
        do something
    except:
        do something
    else:
        do something
    finally
        do something
    

    相关文章

      网友评论

          本文标题:Python的异常处理(二)

          本文链接:https://www.haomeiwen.com/subject/vbkxdttx.html