Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7

上传人:E**** 文档编号:89346250 上传时间:2019-05-23 格式:PPT 页数:33 大小:88.50KB
返回 下载 相关 举报
Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7_第1页
第1页 / 共33页
Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7_第2页
第2页 / 共33页
Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7_第3页
第3页 / 共33页
Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7_第4页
第4页 / 共33页
Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7》由会员分享,可在线阅读,更多相关《Java 2简明教程(第2版) 教学课件 ppt 作者 Java2-7(33页珍藏版)》请在金锄头文库上搜索。

1、1,第7章 异常处理,本章主要讲述如下内容: 异常的层次结构; 异常处理语句; 自定义异常; 异常处理中常用的调试方法。,2,7.1 异常的层次结构,Throwable是异常类的根节点,定义在java.lang包,它的子类也定义在该包中; Error代表系统错误类,由系统直接处理; Exception类及其子类是在程序中可捕捉到的异常。 见图7.1,4,7.1 异常的层次结构(续),1. java.lang.ArithmeticException 0作除数(包括模),将产生这类异常。 例如:int x=0 , y; y=100/x ;,2. java.lang.ArrayIndexOutOfB

2、oundsException 例如:int a =new int10; a10=0;,5,7.1 异常的层次结构(续),3. java.lang.ArrayStoreException 例如:int a =new int10; boolean b =new boolean10; System.arraycopy(a,0,b,3,6); /有异常,4. java.lang.ClassCastException 例如: Object obj=new Object( ); int a =(int ) (obj);,6,7.1 异常的层次结构(续),5. java.lang.IndexOutOfBou

3、ndsException 例如:char ch=“ABC“.charAt(99); 注意: 2 是5的子类。,6. java.lang.NegativeArraySizeException 例如:int a =new int-10;,7,7.1 异常的层次结构(续),7. java.lang.NullPointerException 例如:int a =null ; System.out.print(a.length);,8,7.2 异常处理语句,缺省的异常处理的情况,例如:,public class DefaultException / 程序7-1 public static void ma

4、in(String args ) int a,b=0; a=2/b; / 此处有异常 System.out.println(“a=”+a); / 不运行此行 ,9,7.2 异常处理语句(续),异常处理语句有try、catch、finally、throw和throws。异常处理的形式为:,try 程序执行体 catch(异常类型1 异常对象1) 异常处理程序体1 catch(异常类型2 异常对象2) 异常处理程序体2 finally 异常处理结束前的执行程序体 ,10,7.2.1 try和catch语句,try语句指明可能产生异常的代码段; catch语句在try语句之后,用于捕捉异常,一个tr

5、y语句可以有多个catch语句与之匹配。 异常处理以后,程序从try语句代码段后继续执行。例如:程序7-2。,public class TryCatchTest / 程序7-2 public static void main(String args ) int a=99,b=0,c; try System.out.println(“产生异常之前“); c=a/b; / 该行有异常 System.out.println(“产生异常之后“); catch(ArrayIndexOutOfBoundsException e) System.out.println(“处理下标越界异常“); catch(

6、ArithmeticException e) System.out.println(“处理算术异常“); System.out.println(“异常处理结束“); ,12,7.2.1 try和catch语句(续),注意:用catch语句捕捉异常时,若找不到相匹配的catch语句,将执行缺省的异常处理。例如:,int a=99,b=0,c; try c=a/b; /产生的异常和捕捉的异常类型不一致 catch(ArrayIndexOutOfBoundsException e) System.out.println(“处理异常“); ,13,7.2.1 try和catch语句(续),注意:当有多

7、个catch语句时,系统依照先后顺序逐个检查 。例如:,try c=a/b; catch(ArithmeticException e) System.out.println(“Divided by zero“); catch(RuntimeException e) System.out.println(“Divided by zero“); ,14,7.2.2 finally语句,无论是否产生异常,finally语句指明的代码一定被执行。例如:,public class testFinally / 程序7-3 public static void main(String args ) int

8、a,b=0; for(int i=0;i=3;i+) System.out.println(“Test No: “+(i+1);,try switch(i) case 0: a=3/b; break; case 1: int c =new int10; c10=0; break; case 2: char ch=“ABC“.charAt(99); break; case 3: return; ,catch(ArithmeticException e) System.out.println(“零作除数!”); catch(ArrayIndexOutOfBoundsException e) Sys

9、tem.out.println(“数组下标越界!“); catch(IndexOutOfBoundsException e) System.out.println(“下标越界!“); finally System.out.println(“在finally块中!“); ,程序运行结果: Test No: 1 零作除数! 在finally块中! Test No: 2 数组下标越界! 在finally块中! Test No: 3 下标越界! 在finally块中! Test No: 4 在finally块中!,18,7.2.3 throw语句,throw语句用于指出当前行有异常,当程序执行到thr

10、ow语句时,流程就转到相匹配的异常处理语句,所在的方法也不再返回值。 throw语句可以将异常对象提交给调用者,以进行再次处理。例如:程序7-4。,public class ThrowException / 程序7-4 public static void Test( ) try int c =new int10; c10=0; catch(ArrayIndexOutOfBoundsException e) System.out.println(“t 数组下标越界!“); throw e; / 抛出点 /System.out.println(“t产生异常后!“); ,public static

11、 void main(String args ) try Test( ); catch(IndexOutOfBoundsException e) System.out.println(“t 下标越界!“); finally System.out.println(“t 在finally块中!“); ,程序运行结果: 数组下标越界! 下标越界! 在finally块中!,21,7.2.4 throws语句,throws语句指明方法中可能要产生的异常类型,由调用者进行异常处理。例如:,import java.io.*; / 程序7-6 public class testThrows public st

12、atic String readString( )throws IOException int ch; String r=“; boolean done=false; while(!done) ch=System.in.read( ); if(ch0 | ch=0xd) done=true; else r = r + (char) ch; return r; ,注意,public static void main(String args ) String str; try str=readString( ); catch(IOException e) System.out.println(“产

13、生了输出/输出异常“); return; System.out.println(“整数是:“+Integer.parseInt(str); ,24,7.2.4 throws语句(续),建议:在多人合作写程序时,一个方法中产生的异常,最好在该方法中进行处理,不要将异常传播给其他人处理。,25,7.3 自定义异常类,通过继承Exception类或它的子类,实现自定义异常类; 对于自定义异常,必须采用throw语句抛出异常,这种类型的异常不会自行产生。总体上分为两步:,26,7.3 自定义异常类(续),第1步:定义异常类。例如:,class userException extends Excepti

14、on int n=0; / 计数器 userException( ) n+; userException(String s ) super(s); n+; String show( ) return “自定义异常对象:“+n; ,27,7.3 自定义异常类(续),第2步:定义异常对象,并抛出该对象。 例如:,public class testException / 程序7-7 static void Test( ) throws userException userException e; e=new userException(“自定义异常“ ); throw e; public stati

15、c void main(String args ) try Test( ); catch(userException e) System.out.println(e.show( ); ,29,7.4 异常处理常用调试方法,在程序中增加输出变量的信息。 例如:System.out.println(“x= “ + x); 通过this输出当前对象的状态。 例如:System.out.println(“对象:”+this); 用printstackTrace( ) 输出异常对象的调用栈;用getMessage( )方法获取异常信息;用getClass( )和getName( )获取异常类名 。例如:,class userException extends Exception / 程序7-8 public userException( ) super(“自定义异常“); public class getMessages public static void m1( ) throws userException m2( ); public stati

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号