java的异常处理 exception

上传人:第*** 文档编号:49191208 上传时间:2018-07-25 格式:PPT 页数:21 大小:881KB
返回 下载 相关 举报
java的异常处理 exception_第1页
第1页 / 共21页
java的异常处理 exception_第2页
第2页 / 共21页
java的异常处理 exception_第3页
第3页 / 共21页
java的异常处理 exception_第4页
第4页 / 共21页
java的异常处理 exception_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《java的异常处理 exception》由会员分享,可在线阅读,更多相关《java的异常处理 exception(21页珍藏版)》请在金锄头文库上搜索。

1、T12 异常回顾 区分 Java 应用程序和 Java Applet Applet 的生命周期 如何将参数传递给 Applet 如何在 Applet中插入多媒体文件2目标异常的概念运用 try 、catch 和 finally 处理异常throw 和 throws编写和使用自定义异常3什么是异常?4public class ExceptionRaised public ExceptionRaised() public int calculate( int operand1, int operand2) int result = operand1 / operand2; return resul

2、t;public static void main(String args) ExceptionRaised obj = new ExceptionRaised();int result = obj.calculate(9, 0);System.out.println(result); 异常情况异 常程序突然终止并将控制交 给操作系统在运行时发生的错误 处理异常:传统思路5 IF B = 0 GO TO ERROR C = A / B PRINT C GO TO EXITERROR:处理异常的块 “以零作除数,代码导致错误”DISPLAY EXIT: END处理运行时错误的伪代码处理异常6手动

3、引发异常指定方法所忽略的异常tryfinallycatchthrowsthrow要监控的程序语句包含在此块中捕获和处理 各种异常释放资源等Java异常类 7文件结束EOFException找不到文件FileNotFoundExceptionI/O 异常的根类IOException数字转化格式异常,比如字符串到 float 型数字的转换无效NumberFormatException不能加载所需的类ClassNotFoundException方法接收到非法参数IllegalArgumentException数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundException尝

4、试访问 null 对象成员NullPointerException许多 java.lang 异常的基类RuntimeException异常层次结构的根类Exception算术错误情形,如以零作除数ArithmeticException线程中断InterruptedException说 明异 常使用try和catch捕获异常8trycatch异常执行 catch 后程序 继续正常运行程序控制引发代码块单 元try和catch代码示例9演示:示例 1q try 和 catch 块的用法class ExceptionRaised /* 构造方法. */public ExceptionRaised()

5、 /* 这个方法运行时将会产生一个异常.* param operand1 除法中的分子* param operand2 除法中的分母* return int 返回除法的结果*/public int calculate(int operand1, int operand2) int result = operand1 / operand2; return result; public class ArithmeticException /* 构造方法. */public ArithmeticException() public static void main(String args) Exce

6、ptionRaised obj = new ExceptionRaised();try /* 定义变量 result 以存储结果. */int result = obj.calculate(9, 0);System.out.println(result);catch (Exception e) System.err.println(“发生异常:“ + e.toString();e.printStackTrace(); finally10try try 块块finally finally 块块catch catch 块块 无异常异常try、catch 和 finally 块的执行流程异常处理的一

7、般形式try/ 要监控错误的代码块methodGeneratingException(); catch (Exception e) / Exception e 的异常处理程序 finally/ 在 try 结束前要执行的代码块cleanup(); 11多重 catch 一段代码可能会生成多个异常 当引发异常时,会按顺序来查看每个 catch 语句,并执 行第一个类型与异常类型匹配的语句 执行其中的一条 catch 语句之后,其他的 catch 语 句将被忽略 12try. catch(ArrayIndexOutOfBoundsException e) catch(Exception e) 异常

8、类的继承结构Throwable 具有两个子类,它们是Exception:处理用户程序应当捕获的异常 情况Error:Error 类的异常为内部错误,因此 在正常情况下不期望用户的程序捕获它们 13ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatExceptionAWTError多重catch中的顺序问题 使用多重 catch 语句时,异常子类一定要位于异常父类 之前 14try. catch(Excepti

9、on e) catch(ArrayIndexOutOfBoundsException e) 多重catch中的顺序问题15演示:示例 2q 多重catch的使用class ExceptionCode /*构造方法.*/protected ExceptionCode() /*这个方法将将零作除数.*/public void calculate() try int num = 0;int num1 = 42 / num;catch (Exception e) System.out.println(“父类异常 catch 子句“);catch (ArithmeticException ae) / 错

10、误 不能到达的代码System.out.println(“这个子类的父类是“+ “exception 类,且不能到达“); class UnreachableCode /*构造方法.*/protected UnreachableCode() /* 类和应用程序的唯一进入点.* param args 字符串参数的数组*/public static void main(String args) ExceptionCode obj = new ExceptionCode();obj.calculate(); 嵌套 try catch 16如果内层 try 没有相应的 catch,则检查外层 catc

11、h class NestedException /* 构造方法。 */protected NestedException() /* 这个方法检测数字的格式。* param argument 用于存储 args 的值。*/public test(String argumnet) try int num = Integer.parseInt(args1);/* 嵌套 try 块。 */try int numValue = Integer.parseInt(args0);System.out.println(“args0 + “的平方是 “ + numValue * numValue); catch

12、 (NumberFormatException nb) System.out.println(“不是一个数字! “); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“请输入数字!“);/*main方法*/public static void main(String args) NestedException obj = new NestedException();obj.test(args0);因此需要嵌套 异常处理程序使用throw抛出异常17语句 3throw throw 异常异常引发的异常引发的异常停止异常处理

13、程序可执行程序语句语句 1语句 2throw和throws的比较18处理异常被调用的方法调用方法处理异常忽略可能的异常防止出现 异常并处理异常type calledmethod-name (parameter-list)throws exception-list / body of method type callingmethod-name try / statementscalledmethod-name(); catch(Exception e) /statements 用户自定义异常自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能表达程序 中发生的所有错误。有时

14、会需要创建用户自定 义异常 自定义异常需要继承Exception 及其子 类19创建和使用用户自定义异常20class ArraySizeException extends NegativeArraySizeException /* 构造方法。 */ArraySizeException() super(“您传递的数组大小非法“); 示例: 示例 6q 创建用户自定义异常 q 继承 Exception 或其子类class ExceptionClass ExceptionClass(int val) size = val;try checkSize(); catch (ArraySizeExcep

15、tion e) System.out.println(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 UserDefinedExceptions /* 构造方法. */protected UserDefinedExceptions() /* 类和应用程序的唯一入口点.* param arg 字符串参数的数组*/public static void main(String arg) ExceptionClass obj = new ExceptionClass(Integer.parseInt(arg0); 总结 异常是运行时发生的错误 可以使用

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

当前位置:首页 > 中学教育 > 职业教育

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