Java第十二章_第1页
Java第十二章_第2页
Java第十二章_第3页
Java第十二章_第4页
Java第十二章_第5页
已阅读5页,还剩51页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308071第12章 异常处理Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308072动 因当一个程序运行时出现一个运行时错误时,这个程序就会异常终止 。该如何处理这个运行时错误,

2、以使程序可以继续运行或者平稳终止呢? 这就是本章要介绍的主题。 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308073学习目标F了解异常和异常处理的概貌(第12.2节)。F探究使用异常处理的优点(第12.3节)。F区别异常的类型: Error (致命的) 和 Exception (非致命的), 以及必检和免检异常(第12.4节)。 F在方法头中声明异常(第12.5.1节)。F在方法中抛出异常(第12.5.2

3、节)。F编写try-catch 块处理异常(第12.5.3节)。F解释异常是如何传播的(第12.5.3节)。F在try-catch块中使用finally子句(第12.6节)。F只为非预期错误使用异常(第12.7节)。F在catch 块中重新抛出异常(第12.8节)。F创建链式异常(第12.9节)。F定义自定制的异常类(第12.10节)。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308074Liang, In

4、troduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807DevideTest类Fpublic class devideTestF public static void main(String args)F DevideInt di=new DevideInt();Fint result;Fresult=di.devide(3,0); F System.out.println(the result is + result);F F

5、 System.out.println(program is running here.);FF该条语句执该条语句执行了吗?行了吗?Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807对DevideTest类进行如下修改Fpublic class devideTestF public static void main(String args)FtryFDevideInt di=new DevideInt()

6、;Fint result;Fresult=di.devide(3,0); F System.out.println(the result is + result);F catch(ArithmeticException e)F System.out.println(e.toString();F F System.out.println(program is running here.);FF该条语句执该条语句执行了吗?行了吗?Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education,

7、Inc. All rights reserved. 0132130807说明F我们看到,当我们在程序中加了红色的代码后,在出现了异常后,程序没有异常中止,而是正常的继续运行。为什么会这样呢?F我们用trycatch语句对程序中可能出现异常的语句进行了处理Ftry。Fcatch(Exception e)F。可能出现异常的可能出现异常的语句语句发生异常后的处发生异常后的处理语句理语句Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

8、. 0132130807FtryFDevideInt di=new DevideInt();Fint result;Fresult=di.devide(3,0); F System.out.println(the result is + result);F catch(ArithmeticException e)F System.out.println(e.toString();F F System.out.println(program is running here.);FF将该语句改为将该语句改为result=mobj.devide(3,1)执行过程执行过程又如何?又如何?这条语这条语句

9、还会句还会执行吗执行吗?Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807catch语句块F当try代码块中的程序发生了异常,系统将这个异常发生的代码行号,类别等信息封装到一个对象中,并将这个对象传递给catch代码块 catch(Exception e)System.out.println(e.getMessage(); FException就是try代码块传递给catch代码块的对象类型,e就是异常对

10、象名。F问题一:e可以改为其他的名字吗?Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080710异常处理概述显示运行时错误使用一条if语句来处理它如果在方法被调用时发生运行错误该怎么办?Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0

11常处理的优势现在,你看到了使用异常处理的优点。它能使方法抛出一个异常给它的调用者。这个调用者可以处理该异常。如果没有这个能力,那么被调用的方法就必须自己处理异常或终止该程序。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080712处理InputMismatchException异常通过处理异常InputMismatchException,程序将可以继续读取输入直到它是正确的为止。Li

12、ang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080713异常类型 LinkageError Error Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBound

13、sException 更多其它类 更多其它类 更多其它类 IllegalArgumentException Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080714系统错误 LinkageError Error Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object

14、ArithmeticException NullPointerException IndexOutOfBoundsException 更多其它类 更多其它类 更多其它类 IllegalArgumentException 系统错误(System errors) 是由Java虚拟机抛出的,它用Error类表示。Error类描述的是内部系统错误。这样的错误很少发生。如果发生,除了通知用户以及尽量稳妥地终止程序外,几乎什么也不能做。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, I

15、nc. All rights reserved. 013213080715异 常 LinkageError Error Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException 更多其它类 更多其它类 更多其它类 IllegalArgumentException 异常(Exception) 描述的是由程序和外部环境所引起的错

16、误,这些错误能被程序捕获和处理。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080716运行时异常 LinkageError Error Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerExcept

17、ion IndexOutOfBoundsException 更多其它类 更多其它类 更多其它类 IllegalArgumentException 运行时异常(RuntimeException) 是程序设计错误所引起的,例如,错误的类型转换、访问越界数组或数值错误。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080717必检异常和免检异常RuntimeException、Error以及它们的子类都被称为免检异

18、常(unchecked exceptions)。所有其它异常都被称为必检异常(checked exceptions),意思是编译器会强制程序员检查并处理它们。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080718免检异常在大多数情况下,免检异常都会反映出程序设计中不可恢复的逻辑错误。例如:如果通过一个引用变量访问一个对象之前并未将一个对象赋值给它,就会抛出一个 NullPointerException异常

19、;如果访问一个数组的界限外的元素,就会抛出IndexOutOfBoundsException异常。这些都是程序中必须纠正的逻辑错误。免检异常可能在程序的任何一个地方出现。为避免过多地使用try-catch 块,Java语言不允许编写代码捕获或声明免检异常。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080719免检异常 LinkageError Error Throwable ClassNotFoundEx

20、ception VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException 更多其它类 更多其它类 更多其它类 IllegalArgumentException 免检异常。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

21、013213080720声明、抛出和捕获异常 method1() try invoke method2; catch (Exception ex) Process exception; method2() throws Exception if (an error occurs) throw new Exception(); 捕获异常 抛出异常 声明异常 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807

22、21声明异常每个方法都必须声明它可能抛出的必检异常的类型。这被称为声明异常(declaring exceptions)。 public void myMethod() throws IOExceptionpublic void myMethod() throws IOException, OtherExceptionLiang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080722抛出异常 当程序检测一个错误时,程序

23、可以创建一个恰当的异常类型的实例并抛出它。这就被称为抛出一个异常(throwing an exception)。这里有一个例子:throw new TheException(); TheException ex = new TheException();throw ex;Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080723抛出异常举例 /* Set a new radius */ public void

24、 setRadius(double newRadius) if (newRadius = 0) radius = newRadius; else throw new IllegalArgumentException( Radius cannot be negative); Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080724捕获异常try statements; / Statements that ma

25、y throw exceptionscatch (Exception1 exVar1) handler for exception1;catch (Exception2 exVar2) handler for exception2;.catch (ExceptionN exVar3) handler for exceptionN; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080725捕获异常 main

26、method . try . invoke method1; statement1; catch (Exception1 ex1) Process ex1; statement2; method1 . try . invoke method2; statement3; catch (Exception2 ex2) Process ex2; statement4; method2 . try . invoke method3; statement5; catch (Exception3 ex3) Process ex3; statement6; 在 method3.中抛出一个异常 调用栈 mai

27、n method main method method1 main method method1 main method method1 method2 method2 method3 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080726捕获或声明必检异常Java强迫程序员处理必检异常。如果方法声明了一个必检异常(即Error或Runtime Exception之外的异常),就必须在try-catch块

28、中调用它,或者在调用方法中声明要抛出异常。例如:假定方法p1调用方法p2,而p2 可能会抛出一个必检异常(例如: IOException),就必须编写如图(a)和(b)所示代码。 void p1() try p2(); catch (IOException ex) . (a) (b) void p1() throws IOException p2(); Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 01321308072

29、7举例:声明、抛出和捕获异常F目标:本例改写第8章中定义的Circle类的setRadius方法来演示如何声明、抛出和捕获异常。如果半径是负数,那么新的setRadius方法就会抛出一个异常。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080728重新抛出异常try statements;catch(TheException ex) perform operations before exits; thro

30、w ex;Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080729finally子句try statements;catch(TheException ex) handling ex; finally finalStatements; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, In

31、c. All rights reserved. 013213080730跟踪程序执行动 画try statements;catch(TheException ex) handling ex; finally finalStatements; Next statement;假设语句中没有出现异常Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080731跟踪程序执行动 画try statements;catch(

32、TheException ex) handling ex; finally finalStatements; Next statement;finally子句总是会被执行Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080732跟踪程序执行动 画try statements;catch(TheException ex) handling ex; finally finalStatements; Next st

33、atement;执行这个方法的下一条语句Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080733跟踪程序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; finally finalStatements; Next statement;假设在statement2中有Exception1类型的异常被抛出Li

34、ang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080734跟踪程序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; finally finalStatements; Next statement;这个异常被处理Liang, Introduction to Java Programming, Eighth Ed

35、ition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080735跟踪程序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; finally finalStatements; Next statement;final块总是会被执行Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All

36、 rights reserved. 013213080736跟踪程序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; finally finalStatements; Next statement;现在执行方法后的下一条子句Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080737跟踪程序执行动 画try

37、 statement1; statement2; statement3;catch(Exception1 ex) handling ex; catch(Exception2 ex) handling ex; throw ex;finally finalStatements; Next statement;statement2抛出一个Exception2类型的异常Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130

38、80738跟踪程序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; catch(Exception2 ex) handling ex; throw ex;finally finalStatements; Next statement;处理异常Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080739跟踪程

39、序执行动 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; catch(Exception2 ex) handling ex; throw ex;finally finalStatements; Next statement;执行final块Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080740跟踪程序执行动

40、 画try statement1; statement2; statement3;catch(Exception1 ex) handling ex; catch(Exception2 ex) handling ex; throw ex;finally finalStatements; Next statement;重新抛出异常并把控制权交给调用者Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080741使用异

41、常时注意事项使用异常时注意事项F异常处理将错误处理代码从正常的程序设计任务中分离开,这样,程序会更易阅读和修改。但是,应该注意,由于异常处理需要初始化新的异常对象,需要从调用栈返回而且还需要沿着方法调用链来传播异常以便找到它的异常处理器,所以,异常处理通常需要更多的时间和资源。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080742何时抛出异常F异常出现在方法中。如果想让该方法的调用者处理异常,你应该创建一

42、个异常对象并将其抛出。如果能在发生异常的方法中处理异常,那么就不需要抛出异常。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080743何时使用异常在代码中,什么时候应该使用try-catch块呢?当必须处理不可预料的错误状况时应该使用它。不要用它来处理简单的、可预料的情况。例如,下面的代码:try System.out.println(refVar.toString();catch (NullPointer

43、Exception ex) System.out.println(refVar is null);Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080744何时使用异常最好用以下代码代替:if (refVar != null) System.out.println(refVar.toString();else System.out.println(refVar is null);Liang, Introduc

44、tion to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080745定义自定制异常类F尽量使用API中的异常类。F如果预定义的类不够则定义自定制的异常类。F通过扩展Exception类或其子类定义自定制的异常类。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080746自定制异常类举例在程序清单12.8中,当半径为负时,setRadius方法会抛出一个异常。假设希望把这个半径传递给处理者,就必须创建一个自定制的异常类。Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 013213080747断言(Assertion)断言就是Java程序中的一条语句,它能够判断你程序中的某个假设。一个

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论