JAVA语言第五章异常

上传人:壹****1 文档编号:569816417 上传时间:2024-07-31 格式:PPT 页数:21 大小:658.52KB
返回 下载 相关 举报
JAVA语言第五章异常_第1页
第1页 / 共21页
JAVA语言第五章异常_第2页
第2页 / 共21页
JAVA语言第五章异常_第3页
第3页 / 共21页
JAVA语言第五章异常_第4页
第4页 / 共21页
JAVA语言第五章异常_第5页
第5页 / 共21页
点击查看更多>>
资源描述

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

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

2、ublic int calculate( int operand1, 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); 异常情况异异 常常程序突然终止并将控制交给操作系统在运行时发生的错误在运行时发生的错误 4ACCP V4.0IF B

3、 IS ZERO GO TO ERRORC = A / BPRINT CGO TO EXITERROR: 处理异常的块 “以零作除数,代码导致错误” DISPLAY EXIT:END处理异常 2-1处理运行时错误的伪代码5ACCP V4.0手动引发异常指定由方法引发的异常 tryfinallycatchthrowsthrow处理异常 2-2要监控的程序语句包含在此块中要监控的程序语句包含在此块中要监控的程序语句包含在此块中要监控的程序语句包含在此块中以合理的方式以合理的方式以合理的方式以合理的方式捕获和处理异常捕获和处理异常捕获和处理异常捕获和处理异常释放资源等释放资源等释放资源等释放资源等6

4、ACCP V4.0Java异常类 文件结束文件结束EOFException找不到文件找不到文件FileNotFoundExceptionI/O 异常的根类异常的根类IOException数字转化格式异常,比如字符串到数字转化格式异常,比如字符串到 float 型数字的转换无效型数字的转换无效NumberFormatException不能加载所需的类不能加载所需的类ClassNotFoundException方法接收到非法参数方法接收到非法参数IllegalArgumentException数组大小小于或大于实际的数组大小数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundE

5、xception尝试访问尝试访问 null 对象成员对象成员NullPointerException许多许多 java.lang 异常的基类异常的基类RuntimeException异常层次结构的根类异常层次结构的根类Exception算术错误情形,如以零作除数算术错误情形,如以零作除数ArithmeticException线程中断线程中断InterruptedException说说 明明异异 常常7ACCP V4.0try 和 catch 块 2-1trycatch异常异常执行 catch 后程序继续正常运行程序控制 引发代码块单单 元元8ACCP V4.0try 和 catch 块 2-2

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

7、ception /* 构造方法. */ 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.toString(); e.printStackTrac

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

9、查看每个 catch 语句,并执行第一个类型与异常类型匹配的语句q执行其中的一条 catch 语句之后,其他的 catch 语句将被忽略 try . catch(ArrayIndexOutOfBoundsException e) catch(Exception e) 12ACCP V4.0ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatException异常类的层次结构qThrowable 具有两个子类,它

10、们是qException:处理用户程序应当捕获的异常情况qError:Error 类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们 AWTError13ACCP V4.0多重 catch 块3-2q使用多重 catch 语句时,异常子类一定要位于异常父类之前 try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) 14ACCP V4.0多重 catch 块3-3演示:示例 2q多重catch的使用class ExceptionCode /*构造方法.*/ protected ExceptionCode() /

11、*这个方法将将零作除数.*/ public 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() /* * 类和应用程序的唯一

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

13、ic test(String argumnet) 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.p

14、rintln(“请输入数字!); /*main方法*/ public static void main(String args) NestedException obj = new NestedException(); obj.test(args0); 因此需要嵌套异常处理程序16ACCP V4.0使用 throw 和 throws 2-1 语句语句 3throw throw 异常异常异常异常引发的异常引发的异常停止停止异常处理程序可执行程序语句可执行程序语句语句语句 1语句语句 217ACCP V4.0使用 throw 和 throws 2-2处理异常被调用的方法被调用的方法调用方法调用方法

15、处理异常可能会导致异常防止被调用的方法出现异常并处理异常type calledmethod-name(parameter-list) throws exception-list / body of methodtype callingmethod-name try / statements calledmethod-name();catch(Exception e) /statements18ACCP V4.0用户自定义异常 2-1q自定义异常概念q使用自定义异常的时候qJavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常 q自定义异常需要继承Excep

16、tion 及其子类19ACCP V4.0class ArraySizeException extends NegativeArraySizeException /* 构造方法。 */ ArraySizeException() super(“您传递的数组大小非法); 用户自定义异常 2-2示例: 示例 6q创建用户自定义异常q继承 Exception 或其子类 class ExceptionClass ExceptionClass(int val) size = val; try checkSize(); catch (ArraySizeException e) System.out.print

17、ln(e); /* 声明变量以存储数组的大小和元素. */ private int size; private int array; /* 检查数组长度的方法. * 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 UserDe

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

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

最新文档


当前位置:首页 > 办公文档 > 工作计划

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