异常JavaCore

上传人:lcm****801 文档编号:54926256 上传时间:2018-09-22 格式:PPT 页数:31 大小:1.56MB
返回 下载 相关 举报
异常JavaCore_第1页
第1页 / 共31页
异常JavaCore_第2页
第2页 / 共31页
异常JavaCore_第3页
第3页 / 共31页
异常JavaCore_第4页
第4页 / 共31页
异常JavaCore_第5页
第5页 / 共31页
点击查看更多>>
资源描述

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

1、异常,,厦门卓越培训中心,教学内容,定义异常 使用 try,catch, and finally 描述异常类 识别公共异常 在程序中处理自定义的异常,什么是异常?,3,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 E

2、xceptionRaised();int result = obj.calculate(9, 0);System.out.println(result); ,异常情况,异 常,程序突然终止并将控制交给操作系统,在运行时发生的错误,异常机制简介,在进行程序设计时,错误的产生是不可避免的,如何处理错误?把错误交给谁去处理?程序又该如何从错误中恢复?这是任何程序设计语言都要解决的问题。 所谓错误,是在程序运行过程中发生的异常事件,比如除0溢出、数组越界、文件找不到等,这些事件的发生将阻碍程序的正常运行。为了增加程序的强壮性,程序设计时,必须考虑到可能发生的异常情况并做出相应的处理。,意外情况的处理思

3、路,传统方式 用if语句进行判断,并作相应的个理 现代的方式 抛现异常,捕获异常的机制,if 判断法,一直沿用至今,十分普遍,行之有效,String s = “abdcd”; int n = s.indexOf(“+”); if(n0)/ 处理意外情况 String s1 = s.substring(0,n);,现在处理异常的常用方法, IF B IS ZERO GO TO ERROR C = A / B PRINT C GO TO EXITERROR:处理异常的块 “以零作除数,代码导致错误”DISPLAY EXIT: END,处理运行时错误的伪代码,现在处理异常的常用方法,手动引发异常,指

4、定由方法引发的异常,try,finally,catch,throws,throw,异常处理的优点,把错误处理代码从常规代码中分离出来 按错误类型和差别分组 对无法预测的错误的捕获和处理 克服了传统方法的错误信息有限的问题 把错误传播给调用堆栈,常出现异常,文件结束,EOFException,找不到文件,FileNotFoundException,I/O 异常的根类,IOException,数字转化格式异常,比如字符串到 float 型数字的转换无效,NumberFormatException,不能加载所需的类,ClassNotFoundException,方法接收到非法参数,IllegalAr

5、gumentException,数组大小小于或大于实际的数组大小,ArrayIndexOutOfBoundException,尝试访问 null 对象成员,NullPointerException,许多 java.lang 异常的基类,RuntimeException,异常层次结构的根类,Exception,算术错误情形,如以零作除数,ArithmeticException,线程中断,InterruptedException,说 明,异 常,异常语法,try语句;调用方法(); catch(异常类型 e)处理异常 catch(异常类型 e)处理异常 ,try 块表示受到监控的代码。其中一但发生

6、情况,就会按照是哪种情况,跳到catch块中执行,e 是产生的异常对象的名字。每个异常发生的时候,都会有对应的异常对象。它记录了异常的详细信息,异常实例,try 和 catch 块的用法,class ExceptionRaised /* 构造方法. */public ExceptionRaised() /* 这个方法运行时将会产生一个异常.* param operand1 除法中的分子* param operand2 除法中的分母* return int 返回除法的结果*/public int calculate(int operand1, int operand2) int result =

7、 operand1 / operand2; return result; ,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)

8、System.err.println(“发生异常:“ + e.toString();e.printStackTrace(); ,tryfinally,catch的目的抓住出现的异常情况,使工作维持正轨 finally的目的有某些事情,相当重要,即便是发生了异常,也不应该被跳过,finally到底用在哪?,宋丹丹的失误,try 开冰箱门装入大象关冰箱门 catch 修理大象 ,开冰箱门 装入大象 关冰箱门,可能会出问题,出现问题后,就忘记关冰箱门了吧?,完美的方案,打开冰箱门 try 装入大象 finally 关闭冰箱门 ,打开冰箱门 try 装入大象 catch() 修理大象 finally

9、关闭冰箱门 ,注意,trycatch,tryfinally是两套互无关 系的机构,可以相互嵌套tryfinallycatch只是一种省略的写法。,多重 catch 块,一段代码可能会生成多个异常 当引发异常时,会按顺序来查看每个 catch 语句,并执行第一个类型与异常类型匹配的语句 执行其中的一条 catch 语句之后,其他的 catch 语句将被忽略,try. catch(ArrayIndexOutOfBoundsException e) catch(Exception e) ,多重 catch 块实例,多重catch的使用,class ExceptionCode /*构造方法.*/pro

10、tected ExceptionCode() /*这个方法将将零作除数.*/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 Unreacha

11、bleCode() /* 类和应用程序的唯一进入点.* param args 字符串参数的数组*/public static void main(String args) ExceptionCode obj = new ExceptionCode();obj.calculate(); ,嵌套 try catch 块,class NestedException /* 构造方法。 */protected NestedException() /* 这个方法检测数字的格式。* param argument 用于存储 args 的值。*/public test(String argumnet) try

12、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(“请输入数字!“);/*main方法*/pu

13、blic static void main(String args) NestedException obj = new NestedException();obj.test(args0);,因此需要嵌套 异常处理程序,多重 catch 块注意,使用多重 catch 语句时,异常子类一定要位于异常父类之前,try. catch(Exception e) catch(ArrayIndexOutOfBoundsException e) ,异常的分类,异常类型的差别,Error:系统的严重错误,一般由Java虚拟机监测生成并抛出,它们包括动态链接失败、虚拟机错误、线程死锁等,Java程序对它们一般不

14、做处理。 Exception:一般性程序故障,一般由用户代码或类库生成并抛出,Java程序需要对它们进行处理。 RuntimeException:运行时程序故障,例如被零除、数组下标越界等。这些错误可能在任何方法中产生,并且良好的程序应该可以避免这些错误。,面对异常的两种做法,继承了抽象方法后的两种选择 实现这个抽象的方法 不实现它,自已声明为抽象的自已写的语句中明确含有异常 自己catch它 方法中声明throws XXX,使用 throw 和 throws,语句 3,throw 异常,引发的异常,停止,异常处理程序,可执行程序语句,语句 1,语句 2,使用 throw 和 throws,处

15、理异常,被调用的方法,调用方法,处理异常,可能会导致异常,防止被调用的方法出现 异常并处理异常,type calledmethod-name (parameter-list)throws exception-list / body of method ,type callingmethod-name try / statements calledmethod-name(); catch(Exception e) /statements ,定义自己的异常类,必须从Exception或者它的子类中继承,可以增加成员变量,以存储更丰富的信息。 创建Excpetion对象,与创建普通对象没有区别 抛出

16、自己的异常对象,throw XXX;,用户自定义异常实例,class ArraySizeException extends NegativeArraySizeException /* 构造方法。 */ArraySizeException() super(“您传递的数组大小非法“); ,示例: 示例 6,创建用户自定义异常 继承 Exception 或其子类,class ExceptionClass ExceptionClass(int val) size = val;try checkSize(); catch (ArraySizeException 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; ,

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

当前位置:首页 > 行业资料 > 其它行业文档

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