Python程序设计-清华大学出版社-董付国第8章-异常处理结构与程序调试

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第8章异常处理结构与程序调试8.1什么是异常x,y=10,5a=x/yprintATraceback(mostrecentcalllast):Filepyshell#2,line1,inmoduleprintANameError:name'A'isnotdefined8.1什么是异常10*(1/0)Traceback(mostrecentcalllast):Filestdin,line1,in?ZeroDivisionError:divisionbyzero4+spam*3Traceback(mostrecentcalllast):Filestdin,line1,in?NameError:name'spam'isnotdefined'2'+2Traceback(mostrecentcalllast):Filestdin,line1,in?TypeError:Can'tconvert'int'objecttostrimplicitly8.1什么是异常语法错误和逻辑错误不属于异常,但有些语法错误往往会导致异常,例如由于大小写拼写错误而访问不存在的对象。当Python检测到一个错误时,解释器就会指出当前流已无法继续执行下去,这时候就出现了异常。异常是指因为程序出错而在正常控制流以外采取的行为。异常分为两个阶段:第一个阶段是引起异常发生的错误;第二个阶段是检测并处理阶段。不建议使用异常来代替常规的检查,如if...else判断。应避免过多依赖于异常处理机制。当程序出现错误,python会自动引发异常,也可以通过raise显示地引发异常。8.2Python中的异常类BaseException+--SystemExit+--KeyboardInterrupt+--GeneratorExit+--Exception+--StopIteration+--ArithmeticError|+--FloatingPointError|+--OverflowError|+--ZeroDivisionError+--AssertionError+--AttributeError+--BufferError+--EOFError+--ImportError+--LookupError|+--IndexError|+--KeyError+--MemoryError+--NameError|+--UnboundLocalError+--OSError|+--BlockingIOError|+--ChildProcessError|+--ConnectionError||+--BrokenPipeError||+--ConnectionAbortedError||+--ConnectionRefusedError||+--ConnectionResetError|+--FileExistsError|+--FileNotFoundError|+--InterruptedError|+--IsADirectoryError|+--NotADirectoryError|+--PermissionError|+--ProcessLookupError|+--TimeoutError+--ReferenceError+--RuntimeError|+--NotImplementedError+--SyntaxError|+--IndentationError|+--TabError+--SystemError+--TypeError+--ValueError|+--UnicodeError|+--UnicodeDecodeError|+--UnicodeEncodeError|+--UnicodeTranslateError+--Warning+--DeprecationWarning+--PendingDeprecationWarning+--RuntimeWarning+--SyntaxWarning+--UserWarning+--FutureWarning+--ImportWarning+--UnicodeWarning+--BytesWarning+--ResourceWarning8.2Python中的异常类可以继承Python内置异常类来实现自定义的异常类。classShortInputException(Exception):'''你定义的异常类。'''def__init__(self,length,atleast):Exception.__init__(self)self.length=lengthself.atleast=atleasttry:s=raw_input('请输入--')iflen(s)3:raiseShortInputException(len(s),3)exceptEOFError:print'你输入了一个结束标记EOF'exceptShortInputException,x:print'ShortInputException:输入的长度是%d,长度至少应是%d'%(x.length,x.atleast)else:print'没有异常发生.'8.2Python中的异常类classMyError(Exception):def__init__(self,value):self.value=valuedef__str__(self):returnrepr(self.value)try:raiseMyError(2*2)exceptMyErrorase:print('Myexceptionoccurred,value:',e.value)Myexceptionoccurred,value:4raiseMyError('oops!')Traceback(mostrecentcalllast):Filestdin,line1,in?__main__.MyError:'oops!'8.2Python中的异常类如果自己编写的某个模块需要抛出多个不同的异常,可以先创建一个基类,然后创建多个派生类分别表示不同的异常。classError(Exception):passclassInputError(Error):Exceptionraisedforerrorsintheinput.Attributes:expression--inputexpressioninwhichtheerroroccurredmessage--explanationoftheerrordef__init__(self,expression,message):self.expression=expressionself.message=messageclassTransitionError(Error):Raisedwhenanoperationattemptsastatetransitionthat'snotallowed.Attributes:previous--stateatbeginningoftransitionnext--attemptednewstatemessage--explanationofwhythespecifictransitionisnotalloweddef__init__(self,previous,next,message):self.previous=previousself.next=nextself.message=message8.3Python中的异常处理结构try...except...结构try子句中的代码块放置可能出现异常的语句,except子句中的代码块处理异常。try:try块#被监控的语句exceptException[,reason]:except块#处理异常的语句当需要捕获所有异常时,可以使用BaseException,代码格式如下:try:……exceptBaseException,e:#不建议这样做......#处理所有错误8.3Python中的异常处理结构whileTrue:try:x=int(input(Pleaseenteranumber:))breakexceptValueError:print(Thatwasnovalidnumber.Tryagain...)8.3Python中的异常处理结构except子句可以在异常类名字后指定一个变量。try:raiseException('spam','eggs')exceptExceptionasinst:print(type(inst))#theexceptioninstanceprint(inst.args)#argumentsstoredin.argsprint(inst)#__str__allowsargstobeprinteddirectly,#butmaybeoverriddeninexceptionsubclassesx,y=inst.args#unpackargsprint('x=',x)print('y=',y)class'Exception'('spam','eggs')('spam','eggs')x=spamy=eggs8.3Python中的异常处理结构try...except...else...语句如果try范围内捕获了异常,就执行except块;如果try范围内没有捕获异常,就执行else块。a_list=['China','America','England','France']print'请输入字符串的序号'whileTrue:n=input()try:printa_list[n]exceptIndexError:print'列表元素的下标越界,请重新输入字符串的序号'else:break8.3Python中的异常处理结构forarginsys.argv[1:]:try:f=open(arg,'r')exceptIOError:print('cannotopen',arg)else:print(arg,'has',len(f.readlines()),'lines')f.close()8.3Python中的异常处理结构带有多个except的try结构try:try块#被监控的语句exceptException1:except块1#处理异常1的语句exceptException2:except块2#处理异常2的语句8.3Python中的异常处理结构try:x=input('请输入被除数:')y=input('请输入除数:')z=float(x)/yexceptZeroDivisionError:print'除数不能为零'exceptTypeError:print'被除数和除数应为数值类型'exceptNameError:print'变量不存在'else:printx,'/',y,'=',z8.3Python中的异常处理结构importsystry:f=open('myfile.txt')s=f.readline()i=int(s.strip())exceptOSErroraserr:print(OSerror:{0}.format(err))exceptValueError:print(Couldnotconvertdatatoaninteger.)except:print(Unexpectederror:,sys.exc_info()[0])raise8.3Python中的异常处理结构将要捕获的异常写在一个元组中,可以使用一个excep语句捕获多个异常:importsystry:f=open('myfile.txt')s=f.readline()i=int(s.strip())except(OSError,ValueError,RuntimeError,NameError):pass8.3Python中的异常处理结构tr

1 / 39
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功