Python基礎學習代碼之錯誤和異常

來源:互聯網
上載者:User
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\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 == 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 '\nWhy did you do an EOF on me?'        sys.exit(0)    except:        print '\nSome error/exception occurred.'    print 'done'func13()

以上就是Python基礎學習代碼之錯誤和異常的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.