Java 语言中的异常课件

上传人:我*** 文档编号:145144241 上传时间:2020-09-16 格式:PPT 页数:29 大小:120KB
返回 下载 相关 举报
Java 语言中的异常课件_第1页
第1页 / 共29页
Java 语言中的异常课件_第2页
第2页 / 共29页
Java 语言中的异常课件_第3页
第3页 / 共29页
Java 语言中的异常课件_第4页
第4页 / 共29页
Java 语言中的异常课件_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《Java 语言中的异常课件》由会员分享,可在线阅读,更多相关《Java 语言中的异常课件(29页珍藏版)》请在金锄头文库上搜索。

1、1,第6章 Java语言中的异常,本章主要内容: 异常的概念重点,难点 异常处理语句重点 异常分类了解 自定义异常重点,2,6.1 Java异常处理概述,应用程序运行过程中发生问题是不可避免的现象; 从问题的严重程度上可分为: 不可恢复性错误:硬件故障、网络通讯中断等致命性错误,程序只能选择退出。 可处理的异常:输入/输出错误、算术运算错误等由于程序设计失误造成的错误,应用程序可以对这些异常进行适当的处理。,3,1. 异常的处理过程,“抛出异常”过程:若发生了可处理的异常,正在运行的程序将创建一个异常对象,该异常对象中记录了导致程序运行错误的原因和发生异常时程序的运行状态等信息。 “异常捕获”

2、过程:Java虚拟机(JVM)捕获到异常对象时,将会寻找相应的异常对象的处理代码,若在程序中找到了相应的异常对象处理代码则进行处理;若没有找到,JVM将中断应用程序的运行。,4,2.异常的类层次结构,Java将异常当作类来处理,按层次结构来区别不同的异常。,异常根类:定义在java.lang包,它的子类也定义在该包中,系统错误类:由系统直接处理,异常类:程序中可捕捉到的异常,5,异常的类层次结构,根类Object,6,3.常见的公共异常,1. ArithmeticException 产生时机:0作除数(包括模)时产生次异常。 例如:int x=0 , y; y=100/x ; 2.NullPo

3、interException 产生时机:引用对象为空时产生。 例如:int a =null ; System.out.print(a.length);,此时对象a为空,发生异常,7,3. NegativeArraySizeException 产生时机:数组的大小为负数时产生。 例如:int a =new int-10; 4. ArrayIndexOutOfBoundsException 产生时机:数组下标越界时产生这类异常。 例如:int a =new int10; a10=0;,8,5. ArrayStoreException 产生时机:数组拷贝时类型不匹配。 例如:int a =new i

4、nt10; boolean b =new boolean10; System.arraycopy(a,0,b,3,6); /有异常 6. FileNotFoundException 产生时机:试图存取一个不存在的文件时发生。,9,7. IndexOutOfBoundsException 产生时机:下标越界时产生。 例如:char ch=ABC.charAt(99); 注意: 2 是5的子类。 8. IOException 产生时机:I/O异常,如输入输出异常,已到文件尾等。 9. SecurityException 产生时机:一般在浏览器中运行applet程序进行安全检查时产生,比如IE试图访

5、问本地文件等。,10,6.2 异常示例,简单扩展前面使用过的程序1-1的HelloWorldApp.java程序,循环打印一些信息。程序执行到第4次循环时,会发生异常代码见程序6-1,11,异常分析,public class HelloWorld public static void main (String args) int i = 0; String greetings = Hello world!, No, I mean it!, HELLO WORLD! ; while (i 4) System.out.println (greetingsi); i+; ,i=3时发生数组下标越界异

6、常,程序中断执行退出,12,6.3 异常处理语句,异常处理语句有try、catch、finally、throw和throws。异常处理的形式为:,13,try 可能发生异常的语句; catch(异常类型1 异常对象1) 异常处理语句序列1; catch(异常类型2 异常对象2) 异常处理语句序列2; finally 异常处理结束前的执行语句; ,14,异常处理语句说明,try语句指明可能产生异常的代码段; catch语句在try语句之后,用于捕捉异常,一个try语句可以有多个catch语句与之匹配。 异常处理以后,程序从try语句代码段后继续执行。 无论是否发生捕捉到异常,finally后的代

7、码都一定会执行。,15,异常的捕获顺序,用catch语句捕捉异常时,若找不到相匹配的catch语句,将执行缺省的异常处理。 当有多个catch语句时,系统依照先后顺序逐个检查 。 一般catch语句捕获的异常对象遵循“从子类到父类”的顺序来设计代码。否则,会发生编译问题。,16,while (i 4) try System.out.println (greetingsi); catch (ArrayIndexOutOfBoundsException e) System.out.println(Resetting Index Value); catch (Exception e) System.

8、out.println(e.toString(); finally System.out.println(This is always printed); i+; / while循环结束,添加异常处理后的程序示例6-2,17,while (i 4) try System.out.println (greetingsi); catch (Exception e) System.out.println(Resetting Index Value); catch (ArrayIndexOutOfBoundsException e) System.out.println(e.toString(); f

9、inally System.out.println(This is always printed); i+; / while循环结束,捕获异常时,异常的子类应放在前面,父类放在后面,否则出错。,18,6.5 异常抛出语句(throw和throws),1. throw语句 用于主动抛出异常对象,当程序执行到throw语句时,流程就转到相匹配的异常处理语句,throw所在的方法不再执行。 throw语句可以将异常对象提交给调用者,以对异常进行处理。,19,public class ThrowException / throw语句示例 public static void Test( ) try i

10、nt c =new int10; c10=0; catch(ArrayIndexOutOfBoundsException e) System.out.println(t 数组下标越界!); throw e; / 异常抛出点 System.out.println(t产生异常后!); ,不能到达的语句,20,public static void main(String args ) try Test( ); catch(IndexOutOfBoundsException e) System.out.println(t 下标越界!); finally System.out.println(t 在fi

11、nally块中!); ,程序运行结果: 数组下标越界! 下标越界! 在finally块中!,21,2. throws语句,throws语句用于定义方法时指明方法中可能要产生的异常类型,由调用者进行异常处理。 throws语句后面可以定义可能抛出多个异常类型,用逗号隔开。例如: public void troubleSome( ) throws IOException, RuntimeException 方法体;,22,/ throws语句示例 public class testThrows public static void caculate ( ) throws ArithmeticExc

12、eption int x=5, y=0, z; z=x/y; public static void main(String args ) try caculate( ); catch(ArithmeticException e) System.out.println(“产生了算术运算异常); ,注意,23,6.6 自定义异常类,除了使用系统异常外,程序员可以通过继承Exception类或它的子类自己定义异常类。 对于自定义异常,必须采用throw语句抛出异常,因为这种类型的异常不会自动产生。,24,1 自定义异常步骤,第1步:定义异常类。例如:,class MyException extend

13、s Exception ,方式1: throw new MyException ( ); 方式2: MyException e=new MyException( ); throw e;,第2步:定义异常对象,并抛出该对象。,25,class MyException extends Exception private int detail; MyException (int a) detail = a; public String toString( ) return “MyException”+detail+”; ,2 自定义异常示例,26,class ExceptionDemo static

14、 void compute (int a ) throws MyException System.out.println(Called compute ( + a + .); if (a10) throw new MyException (a); System.out.println(Normal exit); public static void main(String args) try compute(1); compute(20); catch (MyException e) System.out.println(Exception caught + e); ,27,本章小结,异常的概念: Exception 异常处理语句:try catch finally 常见异常 自定义异常 :throw和throws的区别,28,上机作业,编程序创建三种系统异常,并进行处理,每种异常输出相应的出错信息。 编写自定义异常,并进行处理,要求用到throw和throws语句。(参照程序6-3),29,第六章 结束谢谢大家!,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

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