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 () &NBsp; 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 (): &nbsP; 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 ()
This article is from the "XWB" blog, make sure to keep this source http://xiewb.blog.51cto.com/11091636/1792273
Python Basic Learning Code errors and exceptions