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 FL Oat ' except Typeerror:retval = ' object type cannot is convert to float ' return retvaldef func4 (argment): Try:retval = float (argment) except (valueerror,typeerror): retval = ' argment must be a number or numeri C 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 DA Ta processing ' log = open (' E:\\cardlog.txt ', ' w ') Try:ccfile = open (' E:\\cardlog.txt ', ' r ') Txns = CCFI Le.readlines () except Ioerror,e:log.write (' no txns this month\n ') log.close () return ccfile.cl OSE () Total = 0.00 Log.write ("Account log:\n") for eachtxn in Txns:result = Func6 (EACHTXN) if Isin Stance (result,float): Total + = result Log.write (' data...processed\n ') ELSE:LOG.WR Ite (' 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 Asse Rtionerror,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 a EOF on me? ' Sys.exit (0) except:print ' \nsome error/exception occurred. ' print ' Done ' func13 ()
The above is the Python Basic Learning Code error and exception content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!