首页 > 后端开发 > Python教程 > Python基础学习代码之错误和异常

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

黄舟
发布: 2016-12-29 17:23:32
原创
1568 人浏览过

def func1():
    try:
        return float('abc')
    except ValueError,e:
        print e
def func2():
    try:
        astr = 'abc'
        float(astr)
    except ValueError:
        astr = None
    return astr
def func3():
    try:
        astr = 'abc'
        float(astr)
    except ValueError:
        astr = 'count not convert non-number to float'
    return astr
def 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  retval
def func4(argment):
    try:
        retval = float(argment)
    except (ValueError,TypeError):
        retval = 'argment must be a number or numeric string'
    return  retval
def 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  retval
def 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\n')
        log.close()
        return
    ccfile.close()
    total = 0.00
    log.write('account log:\n')
    for eachtxn in txns:
        result = func6(eachtxn)
        if isinstance(result,float):
            total += result
            log.write('data...processed\n')
        else:
            log.write('ignored:%s'%result)
    print '$%.2f newbalance' % total
    log.close()
#if __name__ == '__main__':
#    main()
def func7():
    assert 1 == 0
def func8():
    try:
        assert 0 == 1,'one does not equal zero'
    except AssertionError,e:
        print '%s:%s' % (e.__class__.__name__,e)
#assert
def func9(expr,args=None):
    if __debug__ and not expr:
        raise AssertionError,args
def func10():
    try:
        float('abc')
    except:
        import sys
        exect = sys.exc_info()
        return exect
def func11():
    try:
        f = open('test.txt')
    except:
        return None
    else:
        return f
def func12():
    try:
        raw_input('input data:')
    except (EOFError,KeyboardInterrupt):
        return None
import math,cmath
def safe_sqrt(data):
    try:
        ret = math.sqrt(data)
    except ValueError:
        ret = cmath.sqrt(data)
    return ret
import sys
def func13():
    try:
        s = raw_input('Enter something-->')
    except EOFError:
        print '\nWhy did you do an EOF on me?'
        sys.exit(0)
    except:
        print '\nSome error/exception occurred.'
    print 'done'
func13()
登录后复制

 以上就是Python基础学习代码之错误和异常的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板