第4章 类、对象和接口3

上传人:飞*** 文档编号:8476240 上传时间:2017-08-11 格式:PPT 页数:67 大小:1.38MB
返回 下载 相关 举报
第4章 类、对象和接口3_第1页
第1页 / 共67页
第4章 类、对象和接口3_第2页
第2页 / 共67页
第4章 类、对象和接口3_第3页
第3页 / 共67页
第4章 类、对象和接口3_第4页
第4页 / 共67页
第4章 类、对象和接口3_第5页
第5页 / 共67页
点击查看更多>>
资源描述

《第4章 类、对象和接口3》由会员分享,可在线阅读,更多相关《第4章 类、对象和接口3(67页珍藏版)》请在金锄头文库上搜索。

1、第4章类、对象和接口,4.10 什么是异常4.11 异常的处理4.12 自定义异常类的使用4.13 Class类4.14 包装类,回顾,继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract,目标,理解异常的概念运用 try 块、catch 块和 finally 块处理异常运用多重 catch 块处理异常运用嵌套 try/catch 块处理异常运用关键字 throw 和 throws 处理异常运用JAVA编写和使用自定义异常,异常就是在程序的运行过程中所发生的异常事件,它中断指令的正常执行。Java中提供了一种独特的处理异常的

2、机制,通过异常来处理程序设计中出现的错误。,410什么是异常,public class ExceptionRaised public ExceptionRaised() public 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);

3、System.out.println(result); ,异常情况,异 常,程序突然终止并将控制交给操作系统,在运行时发生的错误,手动引发异常,指定由方法引发的异常,finally,catch,throws,throw,411处理异常,try,Java异常类,文件结束,EOFException,找不到文件,FileNotFoundException,I/O 异常的根类,IOException,数字转化格式异常,比如字符串到 float 型数字的转换无效,NumberFormatException,不能加载所需的类,ClassNotFoundException,方法接收到非法参数,Illegal

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

5、onRaised public ExceptionRaised() public int calculate(int operand1, int operand2) int result = operand1 / operand2; return result; ,public class ArithmeticException /* 构造方法. */ public ArithmeticException() public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); try /* 定义变量

6、 result 以存储结果. */ int result = obj.calculate(9, 0); System.out.println(result); catch (Exception e) System.err.println(“发生异常: + e.toString(); e.printStackTrace(); ,发生异常:java.lang.ArithmeticException: / by zero,java.lang.ArithmeticException: / by zeroat ExceptionRaised.calculate(ArithmeticException.j

7、ava:4)at ArithmeticException.main(ArithmeticException.java:18),finally 块,try 块,finally 块,catch 块,无异常,异常,try、catch 和 finally 块的执行流程,异常处理块的一般形式,try / 要监控错误的代码块 methodGeneratingException(); catch (Exception e) / Exception e 的异常处理程序 finally / 在 try 结束前要执行的代码块 cleanup();,调用抛出异常的方法必须使用异常处理块的一般形式,多重 catch

8、块,一段代码可能会生成多个异常当引发异常时,会按顺序来查看每个 catch 语句,并执行第一个类型与异常类型匹配的语句执行其中的一条 catch 语句之后,其他的 catch 语句将被忽略,try . catch(ArrayIndexOutOfBoundsException e) catch(Exception e) ,Exception,ArithmeticException,NullPointerException,Object,Throwable,Error,ThreadDeath,SQLException,RuntimeException,NumberFormatException,异

9、常类的层次结构,Throwable 具有两个子类,它们是Exception:处理用户程序应当捕获的异常情况Error:Error 类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们,AWTError,多重 catch 块,使用多重 catch 语句时,异常子类一定要位于异常父类之前,try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) ,多重 catch 块,多重catch的使用,class ExceptionCode /*构造方法.*/ protected ExceptionCode() /*这个方法将将零

10、作除数.*/ 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() public static void main(String

11、args) ExceptionCode obj = new ExceptionCode(); obj.calculate(); ,嵌套 try catch 块,class NestedException protected NestedException() public test(String args) try int num = Integer.parseInt(args1); try int numValue = Integer.parseInt(args0); System.out.println(“args0 + “的平方是 + numValue * numValue); catc

12、h (NumberFormatException nb) System.out.println(“不是一个数字! ); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“请输入数字!); public static void main(String args) NestedException obj = new NestedException(); obj.test(“1234”); /使用不同参数对异常进行测试,因此需要嵌套异常处理程序,如果内层 try 没有相应的 catch,则检查外层 catch,使用 throw

13、 和 throws,语句 3,throw 异常,引发的异常,停止,异常处理程序,可执行程序语句,语句 1,语句 2,使用 throw 和 throws,处理异常,被调用的方法,调用方法,处理异常,可能会导致异常,防止被调用的方法出现异常并处理异常,type calledmethod-name(parameter-list) throws exception-list / body of method,type callingmethod-name try / statements calledmethod-name();catch(Exception e) /statements,412用户自

14、定义异常,自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常 自定义异常需要继承Exception 及其子类,class ArraySizeException extends NegativeArraySizeException ArraySizeException() super(“您传递的数组大小非法); ,用户自定义异常,创建用户自定义异常继承 Exception 或其子类,class ExceptionClass private int size; private int array; ExceptionClass(int val) size = val; try checkSize(); catch (ArraySizeException e) System.out.println(e); public void checkSize() throws ArraySizeException if (size 0) throw new ArraySizeException(); array = new intsize; for (int count = 0; count size; count+) arraycount = count + 1; ,

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

最新文档


当前位置:首页 > 高等教育 > 其它相关文档

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