湖南科技职业学院软件职业技术学院 教师课时授课计划 教师姓名: 授课班级: 授课课时:6 课程名称:JAVA程序设计 第 6 课 教学循环号:5-1 课题 异常处理  各教学环节课时分配 讲授  4.4 课时   有指导的实践  1.6 课时   独立实践  0 课时  教学内容 1、处理程序中运行时的错误 2、创建异常类  重点   难点 处理程序中运行时的错误  教具 多媒体  作业 独立实践  授课日期 (校历) 第 3 周星期 5 第 5-8 节   第 4 周星期 1 第 1-2 节   第 周星期 第 节   第 周星期 第 节  课后小结 通过本课的学习,学生掌握了: 异常处理机制 如何实现异常处理  详细教学过程附后 详细教学过程: 一、教学目标陈述(5分钟) 处理程序中运行时的错误 创建异常类 二、JAVA中的异常处理机制(20分钟) 1、异常 程序执行过程中可能出现的异常情况 它们突然终止程序的执行 必须处理异常事件以防止程序执行的突然终止 2、异常类 用于处理异常 在java.lang 包中提供 Throwable 类是异常类层次结构的顶层类 Error 和 Exception类都是从Throwable类派生出来的 三、实例分析 6.D.1 (45*2分钟) 1、问题的陈述(纠正运行时错误) CustomerCollection 类的代码产生一个错误。错误产生之后,程序执行终止。下面给出CustomerCollection 类的代码 模板。你需要控制程序执行使得其执行不会突然终止。 //CustomerCollection.java class Customer { String custName; String custCellNo; String custPackage; int custAge; public void displayDetails() { System.out.println(custName); System.out.println(custCellNo); System.out.println(custPackage); System.out.println(custAge); } } public class CustomerCollection { Customer custObjects[]; public CustomerCollection() { custObjects = new Customer[3]; for(int ctr=0;ctr != 3;ctr++) { custObjects[ctr] = new Customer(); } ::: } public void displayCollection() { for(int ctr=0;ctr!=4; ctr++) { custObjects[ctr].displayDetails(); } } } 此代码模板产生的错误如下:  2、确定任务 识别错误发生时的错误类型和代码段 识别捕获异常 的机制 识别要写的异常处理的代码的位置 识别要显示的错误消息 写出处理异常的代码 保存、编译、及执行程序 3、分析解决问题 识别错误发生时的错误类型和代码段 常见的异常: ArithmeticException NullPointerException ArrayIndexOutOfBoundsException 结果 按问题陈述所指出的,上面程序执行时出现的错误类型是ArrayIndexOutOfBoundsException。错误发生的代码片段是displayCollection() 方法,那里发生超出数组边界的数组访问。 识别捕获异常的机制 异常处理技术:   try 块 – 用来监视可能引起异常的语句 catch 块– 包含在异常发生时要执行的语句的异常处理程序 例子: try { openfile(..); } catch(..) { //process the exception } 多 catch 块 当 try 块有可能引发不同类异常的语句时是必要的 例子: public class TryCatch { public static void main(String args[]) { int array[] = {0,0}; int num1, num2, result = 0; num1 = 100; num2 = 0; try { result = num1/num2; System.out.println(num1 / array[2]); //more statements } catch(ArithmeticException e) { System.out.println("Error... Division by zero"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Error…Out.of.bounds"); } catch (Exception e) { System.out.println("Some other error"); } System.out.println("The result is: " + result); //program proceeds } Nested嵌套的 try 和 catch 块 类似于嵌套构造 try 块可彼此嵌套 catch 块可包含 try-catch块 如果内层 try-catch块没有匹配的catch 处理程序,则对它检查外层的 try 块 结果: 如上所述,捕获异常时必须使用 try 和 catch块 识别需要写异常处理代码的位置 结果 displayCollection()方法内的for循环必须括在try块内。这要跟以一个 catch块。 识别要显示的错误消息 结果 要显示的错误消息是 “Trying to access beyond the length of the array” 编写处理异常的代码 结果 public class CustomerCollection { public void displayCollection() { try { for(int ctr=0;ctr!=4; ctr++) { custObjects[ctr].displayDetails(); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“ Trying to access beyond the length of the array ”); } } } 4、执行验证 保存、编译、及执行程序 动作: 验证:执行此程序时,在消息“Trying to access beyond the length of the array” 和 “All Records displayed”之后显示三个客户的材料 四、指导性练习(45*2分钟) 6.P.1 1、问题陈述(纠正运行时错误) 下面给出ExecutiveCollection 类的代码。此代码正常地编译。执行代码时引发了异常,且执行被终止。使用异常处理技术来调试此代码并保证此代码成功地执行。 2、问题分析 3、验证 五、用户自定义异常(40分钟) 1、用户定义异常的需要 它们用来考虑这种特殊的约束 2、创建用户定义的异常类 可通过扩充Exception 类来创建它们 在实现用户定义的异常时,使用throw和throws 关键字 如果方法可能引发它不处理的异常,它必须指出此异常必须要被调用程序处理。此用throws 语句来做 3、例子 class IllegalValueException extends Exception { public String getMessage() { return “Error in the values supplied to the function”; } } class UserTrial { int val1, val2; public UserTrial(int a, int b) { val1 = a; val2 = b; } void show() throws IllegalValueException { if ((val1 < 0)||(val2 > 0)) throw new IllegalValueException(); System.out.println ("Value 1 = " + val1); System.out.println ("Value 2 = " + val2); } } class ThrowExample { public static void main (String args[]) { UserTrial values = new UserTrial(-1, 1); try { values.show(); } catch (IllegalValueException e) { System.out.println (e.getMessage()); } } } 4、代码说明::?????????????????? 给出的上面代码中,由Exception 类扩充称为llegalValueException的类 UserTrial类有引发称为IllegalValueException 的用户定义的异常的方法 ThrowExample 类中的main()方法创建类UserTrial 的对象并把有错误的值传给其构造符 main()方法的try 块调用 show()方法 show()方法引发一个 exception, 它被main()方法中异常处理程序捕获 catch 块中出现的消息 “Illegal Values: Caught in main”, 显示在屏幕上 六、实例分析 6.D.2 (40分钟) 1、问题陈述(用户定义的异常) 下面给出Customer 类的代码模板。你必须保证购买移动实施包的客户的年龄在20 与60之间。创建一个异常类以防止客户年龄的数据进入的不符合。 public class Customer { int custAge; void setAge(int age){ custAge=age; } // init method // validateAction class } 2、确定任务 识别什么是必须监视的异常情况 识别监视异常情况要用的机制 识别捕获异常情况所需异常类的名 识别监督异常情形的代码需放置的位置 识别要显示的错误消息 编写异常类的代码 编写引发异常的代码 编写捕获异常的代码 保存、编译、及执行程序 3、分析解决问题 识别什么是必须监视的异常情况 结果: 你需保证客户年龄应在20 和60之间 识别监视异常情况要用的机制??????????????? 结果: ??????????????????? 需创建一个异常类以监督客户年龄的数据进入的不符合性 识别捕获异常情况所需异常类的名 结果: 需创建名为 IllegalAgeException 的类以捕获客户年龄数据进入的错误 识别监督异常情形的代码需放置的位置 结果: 你需编写代码以监督Customer 类的setAge()方法中异常情况 识别要显示的错误消息 结果: 当异常出现时要显示的消息是“Invalid Age. The customer should not be provided with a connection.” 编写异常类的代码 动作: public class Customer extends JApplet { //Same as previous code } class ValidateAction implements ActionListener { //Same as previous code } class IllegalAgeException extends Exception { public String getMessage() { return “Invalid Age. The customer should not be provided with a connection”; } } 编写引发异常的代码 代码: public class Customer extends JApplet { int custAge; void setAge(int age) throws IllegalAgeException { if ((age<20) || (age>60)) throw new IllegalAgeException(); custAge=age; } } 编写捕获异常的代码 代码: public void actionPerformed ( ActionEvent evt) { //………… try { custObj.setAge(age); getAppletContext().showStatus(“ Valid entry for customer age”); } catch(IllegalAgeException e) { getAppletContext().showStatus(e.getMessage()); } } 4、执行验证 保存、编译、及执行程序 动作: 打入 年龄的值为10 和 30 并检查是否引发异常 七、小结 (10分钟)