Java程序设计中文13slide_第1页
Java程序设计中文13slide_第2页
Java程序设计中文13slide_第3页
Java程序设计中文13slide_第4页
Java程序设计中文13slide_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter13异常处理1ObjectivesToknowwhatisexceptionandwhatisexceptionhandling(§13.2).Todistinguishexceptiontypes:Error(fatal)vs.Exception(non-fatal),andcheckedvs.uncheckexceptions(§13.2).Todeclareexceptionsinthemethodheader(§13.3).Tothrowexceptionsoutofamethod(§13.3).Towriteatry-catchblocktohandleexceptions(§13.3).Toexplainhowanexceptionispropagated(§13.3).Torethrowexceptionsinatry-catchblock(§13.4).Tousethefinallyclauseinatry-catchblock(§13.5).Toknowwhentouseexceptions(§13.6).Todeclarecustomexceptionclasses(§13.7Optional).Toapplyassertionstohelpensureprogramcorrectness(§13.8).

2语法错误,运行错误,逻辑错误我们学过程序有三类错误:语法错误,运行错误,逻辑错误。语法错误的原因是没有遵循语言的规则,它们可以由编译器检查发现。在程序运行过程中,如果环境发现一个不可能执行的操作,就会出现运行错误。如果程序没有按照预期的方案执行,就会发生逻辑错误不。本章主要介绍应用异常处理来处理运行错误。3运行错误Run4捕捉运行错误Run5异常类6系统错误系统错误由Java虚拟机抛出并在Error类中描述。这种错误很少发生,如果发生,除了通知用户以及尽量稳妥地结束程序外,几乎什么都不能做。7ExceptionsException

描述由程序和外部环境引起的错误,这些错误能通过程序捕获和处理。

8RuntimeExceptionsRuntimeException

描述编程错误,比如不合适的转换。运行异常通常由Java虚拟机抛出。9必检异常和免检异常RuntimeException,Error

和它们的子类都称为免检异常,因为这类异常可能在程序任何地方出现,为避免过多进行异常处理,Java语言不允许编写捕获或声明免检异常的代码。

所有其他异常称为必检异常,意思是指编译器会强制程序员检查并处理它们。

10必检异常和免检异常Uncheckedexception.11声明、抛出、捕捉异常12声明异常每个方法都必须说明它可能抛出的必检异常的类型,这称为声明异常。

publicvoidmyMethod()throwsIOExceptionpublicvoidmyMethod()throwsIOException,OtherException注:如果在父类中方法没有声明异常,那么在子类中不能对其进行覆盖以声明异常。13抛出异常程序检查到一个错误后,创建一个适当类型异常的实例并抛出它,这称为抛出异常。例如,thrownewTheException();TheExceptionex=newTheException();

throwex;注:JavaAPI中的每个异常类至少有两个构造方法:一个无参构造方法和一个带有String参数的构造方法。该参数用于描述异常,称为异常信息,可以使用getMessage()获得。14抛出异常例子

/**Setanewradius*/publicvoidsetRadius(double

newRadius)

throwsIllegalArgumentException{if(newRadius>=0)radius=newRadius;else

thrownewIllegalArgumentException("Radiuscannotbenegative");}15捕捉异常try{statements;//Statementsthatmaythrowexceptions}catch(Exception1exVar1){handlerforexception1;}catch(Exception2exVar2){handlerforexception2;}...catch(ExceptionNexVar3){handlerforexceptionN;}

16捕捉异常注:如果异常不能在当前的方法中捕获,就传给该方法的调用者。这个过程一直重复,直到异常被捕获或被传给main方法。注:异常类可派生出多种子异常类,如果一个catch块能捕获父类异常,它也能捕获所有的子类异常。所以父类的异常块不能出现在子类的catch块前。17捕捉或声明必检异常Java强迫程序员处理必检异常。如果方法声明一个必检异常,必须在try-catch块中调用它或者在调用它的方法中声明抛出异常。例如,假定方法p1调用方法p2,p2可能抛出一个必检异常(如IOException),就要像下图a或b那样编写代码。18Example:声明、抛出和捕捉异常Objective:本例演示如何声明、抛出和捕获异常。改写例子7-3中的Circle2类的setRadius方法,如果半径是负数,则抛出一个异常。TestCircleWithExceptionRunCircleWithException19重新抛出异常try{statements;}catch(TheExceptionex){performoperationsbeforeexits;throwex;}20finally

子句try{statements;}catch(TheExceptionex){handlingex;}finally{

finalStatements;}有时,不论异常是否出现或者是否被捕获,都希望执行某些代码。可用finally子句。在任何情况下,finally块中的代码都会执行,不管try块中是否出现异常或者是否捕获了异常,甚至在到过finally块之前有一个return语句,finally块还是会执行。21TraceaProgramExecutionanimationtry{statements;}catch(TheExceptionex){handlingex;}finally{

finalStatements;}Nextstatement;Supposenoexceptionsinthestatements22TraceaProgramExecutionanimationtry{statements;}catch(TheExceptionex){handlingex;}finally{

finalStatements;}Nextstatement;Thefinalblockisalwaysexecuted23TraceaProgramExecutionanimationtry{statements;}catch(TheExceptionex){handlingex;}finally{

finalStatements;}Nextstatement;Nextstatementinthemethodisexecuted24TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally{

finalStatements;}Nextstatement;SupposeanexceptionoftypeException1isthrowninstatement225TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally{

finalStatements;}Nextstatement;Theexceptionishandled.26TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally{

finalStatements;}Nextstatement;Thefinalblockisalwaysexecuted.27TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}finally{

finalStatements;}Nextstatement;Thenextstatementinthemethodisnowexecuted.28TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}catch(Exception2ex){handlingex;throwex;}finally{

finalStatements;}Nextstatement;statement2throwsanexceptionoftypeException2.29TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}catch(Exception2ex){handlingex;throwex;}finally{

finalStatements;}Nextstatement;Handlingexception30TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}catch(Exception2ex){handlingex;throwex;}finally{

finalStatements;}Nextstatement;Executethefinalblock31TraceaProgramExecutionanimationtry{statement1;statement2;statement3;}catch(Exception1ex){handlingex;}catch(Exception2ex){handlingex;throwex;}finally{

finalStatements;}Nextstatement;Rethrowtheexceptionandcontrolistransferredtothecaller32何时使用异常异常处理可以将错误处理代码从正常的编程任务中分离出来,使程序容易阅读和修改。然而,由于异常处理需要初始化新的异常对象,并重新返回调用堆栈,并且通过方法调用链传播异常,以便捕获异常,所以通常情况下异常处理需要更多的时间和资源。33什么时候抛出异常一个方法出现异常时,如果想让方法的调用者处理异常,应该创建一个异常对象并将其抛出。如果能在发生异常的方法中处理异常,那么就

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论