Python基础学习代码之错误和异常

def func1():    try:        return float('abc')    except ValueError,e:        print edef func2():    try:        astr = 'abc'        float(astr)    except ValueError:        astr = None    return astrdef func3():    try:        astr = 'abc'        float(astr)    except ValueError:        astr = 'count not convert non-number to float'    return astrdef safe_float(argment):    try:        retval = float(argment)    except ValueError:        retval = 'count not convert non-number to float'    except TypeError:        retval = 'object type cannot be convert to float'    return  retvaldef func4(argment):    try:        retval = float(argment)    except (ValueError,TypeError):        retval = 'argment must be a number or numeric string'    return  retvaldef func5(argment):    try:        retval = float(argment)    except ValueError,e:        print e    print type(e)    print e.__class__    print e.__class__.__doc__    print e.__class__.__name__def func6(argment):    try:        retval = float(argment)    except (ValueError,TypeError),e:        retval = str(e)    return  retvaldef main():    'handles all the data processing'    log = open('e:\cardlog.txt','w')    try:        ccfile = open('e:\cardlog.txt','r')        txns = ccfile.readlines()    except IOError,e:        log.write('no txns this month')        log.close()        return    ccfile.close()    total = 0.00    log.write('account log:')    for eachtxn in txns:        result = func6(eachtxn)        if isinstance(result,float):            total += result            log.write('data...processed')        else:            log.write('ignored:%s'%result)    print '$%.2f newbalance' % total    log.close()#if __name__ == '__main__':#    main()def func7():    assert 1 == 0def func8():    try:        assert 0 == 1,'one does not equal zero'    except AssertionError,e:        print '%s:%s' % (e.__class__.__name__,e)#assertdef func9(expr,args=None):    if __debug__ and not expr:        raise AssertionError,argsdef func10():    try:        float('abc')    except:        import sys        exect = sys.exc_info()        return exectdef func11():    try:        f = open('test.txt')    except:        return None    else:        return fdef func12():    try:        raw_input('input data:')    except (EOFError,KeyboardInterrupt):        return Noneimport math,cmathdef safe_sqrt(data):    try:        ret = math.sqrt(data)    except ValueError:        ret = cmath.sqrt(data)    return retimport sysdef func13():    try:        s = raw_input('Enter something-->')    except EOFError:        print 'Why did you do an EOF on me?'        sys.exit(0)    except:        print 'Some error/exception occurred.'    print 'done'func13()

登录后复制

 以上就是python基础学习代码之错误和异常的内容,更多相关内容请关注【创想鸟】(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2281135.html

(0)
上一篇 2025年2月27日 18:26:14
下一篇 2025年2月23日 09:11:49

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论