zoukankan      html  css  js  c++  java
  • 10.3.3 带有多个except 的try语句:

    10.3.3 带有多个except 的try语句:
    
    我们的safe_float()函数已经可以检测到指定的异常了,更聪明的代码能够处理好每一种异常。
    
    这就需要多个except语句,每个except 语句对应一种异常类型。
    
    def safe_float(obj):
        try:
            return float(obj)
        except ValueError,e:
            retval = 'could not convert non-number to float'
            return retval
        except TypeError,e:
            retval ='object type cannot be converted to float'
    a=safe_float('abc')
    print a
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/ddd/a19.py
    could not convert non-number to float
    
    def safe_float(obj):
        try:
            return float(obj)
        except ValueError,e:
            retval = 'could not convert non-number to float'
            return retval
        except TypeError,e:
            retval ='object type cannot be converted to float'
            print 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/a19.py
    <__main__.fun1 object at 0x02297310>
    <class '__main__.fun1'>
    object type cannot be converted to float
    None
    
    

  • 相关阅读:
    linux知识笔记4
    linux知识笔记3
    linux知识笔记2
    linux常用命令笔记1
    计算机网络
    软件测试理论5
    软件测试理论4
    软件测试理论3
    Yarn 常用命令
    mac shell终端编辑命令行快捷键
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349234.html
Copyright © 2011-2022 走看看