资源描述
,异常--运用多重catch块处理异常,2,回顾,继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract,3,目标,理解异常的概念运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常,4,什么是异常?,publicclassExceptionRaised{publicExceptionRaised(){}publicintcalculate(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}publicstaticvoidmain(String[]args){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(String[]args){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(){try{intnum=0;intnum1=42/num;}catch(Exceptione){System.out.println("父类异常catch子句");}catch(ArithmeticExceptionae){//错误–不能到达的代码System.out.println("这个子类的父类是"+"exception类,且不能到达");}}},classUnreachableCode{/**构造方法.*/protectedUnreachableCode(){}/***类和应用程序的唯一进入点.*@paramargs字符串参数的数组*/publicstaticvoidmain(String[]args){ExceptionCodeobj=newExceptionCode();obj.calculate();}},,16,嵌套try–catch块,如果内层try没有相应的catch,则检查外层catch,,classNestedException{/*构造方法。*/protectedNestedException(){}/**这个方法检测数字的格式。*@paramargument用于存储args的值。*/publictest(String[]argumnet){try{intnum=Integer.parseInt(args[1]);/*嵌套try块。*/try{intnumValue=Integer.parseInt(args[0]);System.out.println(“args[0]+“的平方是"+numValue*numValue);}catch(NumberFormatExceptionnb){System.out.println(“不是一个数字!");}}catch(ArrayIndexOutOfBoundsExceptionne){System.out.println(“请输入数字!!!");}}/**main方法*/publicstaticvoidmain(String[]args){NestedExceptionobj=newNestedException();obj.test(args[0]);}},,,,,因此需要嵌套异常处理程序,17,使用throw和throws2-1,语句3,,throw异常,引发的异常,停止,异常处理程序,可执行程序语句,,语句1,语句2,,,,,,,18,,使用throw和throws2-2,,处理异常,,,被调用的方法,调用方法,处理异常,可能会导致异常,防止被调用的方法出现异常并处理异常,,typecalledmethod-name(parameter-list)throwsexception-list{//bodyofmethod},typecallingmethod-name{try{//statementscalledmethod-name();}catch(Exceptione){//statements}},19,用户自定义异常2-1,自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常自定义异常需要继承Exception及其子类,20,classArraySizeExceptionextendsNegativeArraySizeException{/**构造方法。*/ArraySizeException(){super(“您传递的数组大小非法");}},用户自定义异常2-2,示例:示例6,创建用户自定义异常继承Exception或其子类,classExceptionClass{ExceptionClass(intval){size=val;try{checkSize();}catch(ArraySizeExceptione){System.out.println(e);}}/**声明变量以存储数组的大小和元素.*/privateintsize;privateint[]array;/**检查数组长度的方法.*@throws一个ArraySizeException*/publicvoidcheckSize()throwsArraySizeException{if(size<0){thrownewArraySizeException();}array=newint[3];for(intcount=0;count<3;count++){array[count]=count+1;}}},classUserDefinedExceptions{/**构造方法.*/protectedUserDefinedExceptions(){}/***类和应用程序的唯一入口点.*@paramarg字符串参数的数组*/publicstaticvoidmain(String[]arg){ExceptionClassobj=newExceptionClass(Integer.parseInt(arg[0]));}},,21,总结,异常是运行时发生的错误可以使用try、catch、throw、throws和finally来管理Java异常处理。要监控的程序语句包含在try块内catch块中的代码用于捕获和处理异常。在方法返回之前绝对必须执行的代码应放置在finally块中要手动引发异常,使用关键字throw。任何被抛到方法外部的异常都必须用throws子句指定多重catch和嵌套try-catch的使用自定义异常的编写和使用,
展开阅读全文