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
    
    

  • 相关阅读:
    git的使用
    对大学学习的一些看法
    远程连接mysql失败情况总结
    缓存穿透、缓存击穿、缓存雪崩
    Hello Redis
    Celery的简单使用
    git操作
    码云、github同时配置ssh key,解决冲突问题
    阿里云短信验证码服务
    Vue中img标签的src属性绑定的坑
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349234.html
Copyright © 2011-2022 走看看