JAVA语言第五章异常.ppt

上传人:本田雅阁 文档编号:2038440 上传时间:2019-02-07 格式:PPT 页数:21 大小:658.51KB
返回 下载 相关 举报
JAVA语言第五章异常.ppt_第1页
第1页 / 共21页
JAVA语言第五章异常.ppt_第2页
第2页 / 共21页
JAVA语言第五章异常.ppt_第3页
第3页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《JAVA语言第五章异常.ppt》由会员分享,可在线阅读,更多相关《JAVA语言第五章异常.ppt(21页珍藏版)》请在三一文库上搜索。

1、第五章,异 常,2,回顾,继承及其JAVA实现 多态及其JAVA实现 访问修饰符对类成员的访问限制 方法修饰符:static、final、abstract,3,目标,理解异常的概念 运用 try 块、catch 块和 finally 块处理异常 运用多重 catch 块处理异常 运用嵌套 try/catch 块处理异常 运用关键字 throw 和 throws 处理异常 运用JAVA编写和使用自定义异常,4,什么是异常?,public class ExceptionRaised public ExceptionRaised() public int calculate( int operand

2、1, int operand2) int result = operand1 / operand2; return result; public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); int result = obj.calculate(9, 0); System.out.println(result); ,异常情况,异 常,程序突然终止并将控制交给操作系统,在运行时发生的错误,5, IF B IS ZERO GO TO ERROR C = A / B PRINT C GO TO E

3、XIT ERROR: 处理异常的块 “以零作除数,代码导致错误” DISPLAY EXIT: END,处理异常 2-1,处理运行时错误的伪代码,6,手动引发异常,指定由方法引发的异常,try,finally,catch,throws,throw,处理异常 2-2,7,Java异常类,文件结束,EOFException,找不到文件,FileNotFoundException,I/O 异常的根类,IOException,数字转化格式异常,比如字符串到 float 型数字的转换无效,NumberFormatException,不能加载所需的类,ClassNotFoundException,方法接收到

4、非法参数,IllegalArgumentException,数组大小小于或大于实际的数组大小,ArrayIndexOutOfBoundException,尝试访问 null 对象成员,NullPointerException,许多 java.lang 异常的基类,RuntimeException,异常层次结构的根类,Exception,算术错误情形,如以零作除数,ArithmeticException,线程中断,InterruptedException,说 明,异 常,8,try 和 catch 块 2-1,try,catch,异常,执行 catch 后程序 继续正常运行,程序控制,引发,代码

5、块,单 元,9,try 和 catch 块 2-2,演示:示例 1,try 和 catch 块的用法,class ExceptionRaised /* 构造方法. */ public ExceptionRaised() /* * 这个方法运行时将会产生一个异常. * param operand1 除法中的分子 * param operand2 除法中的分母 * return int 返回除法的结果 */ public int calculate(int operand1, int operand2) int result = operand1 / operand2; return result

6、; ,public class ArithmeticException /* 构造方法. */ public ArithmeticException() public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); try /* 定义变量 result 以存储结果. */ int result = obj.calculate(9, 0); System.out.println(result); catch (Exception e) System.err.println(“发生异常:“ + e

7、.toString(); e.printStackTrace(); ,10,finally 块,try 块,finally 块,catch 块,无异常,异常,try、catch 和 finally 块的执行流程,11,异常处理块的一般形式,try / 要监控错误的代码块 methodGeneratingException(); catch (Exception e) / Exception e 的异常处理程序 finally / 在 try 结束前要执行的代码块 cleanup(); ,12,多重 catch 块3-1,一段代码可能会生成多个异常 当引发异常时,会按顺序来查看每个 catch

8、语句,并执行第一个类型与异常类型匹配的语句 执行其中的一条 catch 语句之后,其他的 catch 语句将被忽略,try . catch(ArrayIndexOutOfBoundsException e) catch(Exception e) ,13,Exception,ArithmeticException,NullPointerException,Object,Throwable,Error,ThreadDeath,SQLException,RuntimeException,NumberFormatException,异常类的层次结构,Throwable 具有两个子类,它们是 Excep

9、tion:处理用户程序应当捕获的异常情况 Error:Error 类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们,AWTError,14,多重 catch 块3-2,使用多重 catch 语句时,异常子类一定要位于异常父类之前,try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) ,15,多重 catch 块3-3,演示:示例 2,多重catch的使用,class ExceptionCode /*构造方法.*/ protected ExceptionCode() /*这个方法将将零作除数.*/ publi

10、c void calculate() try int num = 0; int num1 = 42 / num; catch (Exception e) System.out.println(“父类异常 catch 子句“); catch (ArithmeticException ae) / 错误 不能到达的代码 System.out.println(“这个子类的父类是“ + “exception 类,且不能到达“); ,class UnreachableCode /*构造方法.*/ protected UnreachableCode() /* * 类和应用程序的唯一进入点. * param

11、args 字符串参数的数组 */ public static void main(String args) ExceptionCode obj = new ExceptionCode(); obj.calculate(); ,16,嵌套 try catch 块,如果内层 try 没有相应的 catch,则检查外层 catch,class NestedException /* 构造方法。 */ protected NestedException() /* 这个方法检测数字的格式。 * param argument 用于存储 args 的值。 */ public test(String argum

12、net) try int num = Integer.parseInt(args1); /* 嵌套 try 块。 */ try int numValue = Integer.parseInt(args0); System.out.println(“args0 + “的平方是 “ + numValue * numValue); catch (NumberFormatException nb) System.out.println(“不是一个数字! “); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“请输入数字!“);

13、 /*main方法*/ public static void main(String args) NestedException obj = new NestedException(); obj.test(args0); ,因此需要嵌套 异常处理程序,17,使用 throw 和 throws 2-1,语句 3,throw 异常,引发的异常,停止,异常处理程序,可执行程序语句,语句 1,语句 2,18,使用 throw 和 throws 2-2,处理异常,被调用的方法,调用方法,处理异常,可能会导致异常,防止被调用的方法出现 异常并处理异常,type calledmethod-name (par

14、ameter-list) throws exception-list / body of method ,type callingmethod-name try / statements calledmethod-name(); catch(Exception e) /statements ,19,用户自定义异常 2-1,自定义异常概念 使用自定义异常的时候 JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常 自定义异常需要继承Exception 及其子类,20,class ArraySizeException extends NegativeArra

15、ySizeException /* 构造方法。 */ ArraySizeException() super(“您传递的数组大小非法“); ,用户自定义异常 2-2,示例: 示例 6,创建用户自定义异常 继承 Exception 或其子类,class ExceptionClass ExceptionClass(int val) size = val; try checkSize(); catch (ArraySizeException e) System.out.println(e); /* 声明变量以存储数组的大小和元素. */ private int size; private int ar

16、ray; /* 检查数组长度的方法. * throws 一个 ArraySizeException */ public void checkSize() throws ArraySizeException if (size 0) throw new ArraySizeException(); array = new int3; for (int count = 0; count 3; count+) arraycount = count + 1; ,class UserDefinedExceptions /* 构造方法. */ protected UserDefinedExceptions()

17、 /* * 类和应用程序的唯一入口点. * param arg 字符串参数的数组 */ public static void main(String arg) ExceptionClass obj = new ExceptionClass(Integer.parseInt(arg0); ,21,总结,异常是运行时发生的错误 可以使用 try、catch、throw、throws 和 finally 来管理 Java 异常处理。要监控的程序语句包含在 try 块内catch 块中的代码用于捕获和处理异常。在方法返回之前绝对必须执行的代码应放置在 finally 块中 要手动引发异常,使用关键字 throw。任何被抛到方法外部的异常都必须用 throws 子句指定 多重catch 和嵌套try-catch的使用 自定义异常的编写和使用,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1