10.3.4 处理多个异常的except 语句:

10.3.4 处理多个异常的except 语句:

我们还可以在一个except 子句里处理多个异常,
except 语句在处理多个异常时要求异常被放在一个元组里:


def safe_float(obj):
    try:
        retval = float(obj)
    except (ValueError,TypeError):
        retval = 'argument must be a number or numeric string'
        return retval
class fun1(object):
    def __init__(self):
        pass
b=fun1()
print b
print type(b)
a=safe_float(b)
print a

C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/ddd/a20.py
<__main__.fun1 object at 0x022A7310>
<class '__main__.fun1'>
argument must be a number or numeric string


def safe_float(obj):
    try:
        retval = float(obj)
    except (ValueError,TypeError):
        retval = 'argument must be a number or numeric string'
        return retval
class fun1(object):
    def __init__(self):
        pass
b=fun1()
print b
print type(b)
a=safe_float('abc')
print a

C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/ddd/a20.py
<__main__.fun1 object at 0x022A7310>
<class '__main__.fun1'>
argument must be a number or numeric string

原文地址:https://www.cnblogs.com/hzcya1995/p/13349233.html