JAVA语言第五章异常.ppt_第1页
JAVA语言第五章异常.ppt_第2页
JAVA语言第五章异常.ppt_第3页
JAVA语言第五章异常.ppt_第4页
JAVA语言第五章异常.ppt_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

第五章,异常,2,回顾,继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract,3,目标,理解异常的概念运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常,4,什么是异常?,publicclassExceptionRaisedpublicExceptionRaised()publicintcalculate(intoperand1,intoperand2)intresult=operand1/operand2;returnresult;publicstaticvoidmain(Stringargs)ExceptionRaisedobj=newExceptionRaised();intresult=obj.calculate(9,0);System.out.println(result);,异常情况,异常,程序突然终止并将控制交给操作系统,在运行时发生的错误,5,IFBISZEROGOTOERRORC=A/BPRINTCGOTOEXITERROR:处理异常的块“以零作除数,代码导致错误”DISPLAYEXIT:END,处理异常2-1,处理运行时错误的伪代码,6,手动引发异常,指定由方法引发的异常,try,finally,catch,throws,throw,处理异常2-2,7,Java异常类,文件结束,EOFException,找不到文件,FileNotFoundException,I/O异常的根类,IOException,数字转化格式异常,比如字符串到float型数字的转换无效,NumberFormatException,不能加载所需的类,ClassNotFoundException,方法接收到非法参数,IllegalArgumentException,数组大小小于或大于实际的数组大小,ArrayIndexOutOfBoundException,尝试访问null对象成员,NullPointerException,许多java.lang异常的基类,RuntimeException,异常层次结构的根类,Exception,算术错误情形,如以零作除数,ArithmeticException,线程中断,InterruptedException,说明,异常,8,try和catch块2-1,try,catch,异常,执行catch后程序继续正常运行,程序控制,引发,代码块,单元,9,try和catch块2-2,演示:示例1,try和catch块的用法,classExceptionRaised/*构造方法.*/publicExceptionRaised()/*这个方法运行时将会产生一个异常.*paramoperand1除法中的分子*paramoperand2除法中的分母*returnint返回除法的结果*/publicintcalculate(intoperand1,intoperand2)intresult=operand1/operand2;returnresult;,publicclassArithmeticException/*构造方法.*/publicArithmeticException()publicstaticvoidmain(Stringargs)ExceptionRaisedobj=newExceptionRaised();try/*定义变量result以存储结果.*/intresult=obj.calculate(9,0);System.out.println(result);catch(Exceptione)System.err.println(“发生异常:+e.toString();e.printStackTrace();,10,finally块,try块,finally块,catch块,无异常,异常,try、catch和finally块的执行流程,11,异常处理块的一般形式,try/要监控错误的代码块methodGeneratingException();catch(Exceptione)/Exceptione的异常处理程序finally/在try结束前要执行的代码块cleanup();,12,多重catch块3-1,一段代码可能会生成多个异常当引发异常时,会按顺序来查看每个catch语句,并执行第一个类型与异常类型匹配的语句执行其中的一条catch语句之后,其他的catch语句将被忽略,try.catch(ArrayIndexOutOfBoundsExceptione)catch(Exceptione),13,Exception,ArithmeticException,NullPointerException,Object,Throwable,Error,ThreadDeath,SQLException,RuntimeException,NumberFormatException,异常类的层次结构,Throwable具有两个子类,它们是Exception:处理用户程序应当捕获的异常情况Error:Error类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们,AWTError,14,多重catch块3-2,使用多重catch语句时,异常子类一定要位于异常父类之前,try.catch(Exceptione)catch(ArrayIndexOutOfBoundsExceptione),15,多重catch块3-3,演示:示例2,多重catch的使用,classExceptionCode/*构造方法.*/protectedExceptionCode()/*这个方法将将零作除数.*/publicvoidcalculate()tryintnum=0;intnum1=42/num;catch(Exceptione)System.out.println(父类异常catch子句);catch(ArithmeticExceptionae)/错误不能到达的代码System.out.println(这个子类的父类是+exception类,且不能到达);,classUnreachableCode/*构造方法.*/protectedUnreachableCode()/*类和应用程序的唯一进入点.*paramargs字符串参数的数组*/publicstaticvoidmain(Stringargs)ExceptionCodeobj=newExceptionCode();obj.calculate();,16,嵌套trycatch块,如果内层try没有相应的catch,则检查外层catch,classNestedException/*构造方法。*/protectedNestedException()/*这个方法检测数字的格式。*paramargument用于存储args的值。*/publictest(Stringargumnet)tryintnum=Integer.parseInt(args1);/*嵌套try块。*/tryintnumValue=Integer.parseInt(args0);System.out.println(“args0+“的平方是+numValue*numValue);catch(NumberFormatExceptionnb)System.out.println(“不是一个数字!);catch(ArrayIndexOutOfBoundsExceptionne)System.out.println(“请输入数字!);/*main方法*/publicstaticvoidmain(Stringargs)NestedExceptionobj=newNestedException();obj.test(args0);,因此需要嵌套异常处理程序,17,使用throw和throws2-1,语句3,throw异常,引发的异常,停止,异常处理程序,可执行程序语句,语句1,语句2,18,使用throw和throws2-2,处理异常,被调用的方法,调用方法,处理异常,可能会导致异常,防止被调用的方法出现异常并处理异常,typecalledmethod-name(parameter-list)throwsexception-list/bodyofmethod,typecallingmethod-nametry/statementscalledmethod-name();catch(Exceptione)/statements,19,用户自定义异常2-1,自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常自定义异常需要继承Exception及其子类,20,classArraySizeExceptionextendsNegativeArraySizeException/*构造方法。*/ArraySizeException()super(“您传递的数组大小非法);,用户自定义异常2-2,示例:示例6,创建用户自定义异常继承Exception或其子类,classExceptionClassExceptionClass(intval)size=val;trycheckSize();catch(ArraySizeExceptione)System.out.println(e);/*声明变量以存储数组的大小和元素.*/privateintsize;privateintarray;/*检查数组长度的方法.*throws一个ArraySizeException*/publicvoidcheckSize()throwsArraySizeExceptionif(size0)thrownewArraySizeException();array=newint3;for(intcount=0;count3;count+)arraycount=count+1;,classUserDefinedExceptions/*构造方法.*/protectedUserDefinedExceptions()/*类和应用程序的唯一入口点.*paramarg字符串参数的数组*/publicstaticvoidmain(Stringarg)ExceptionClassobj=newExceptionClass(

温馨提示

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

评论

0/150

提交评论