北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第6章 异常处理

第6章异常处理 理解异常和异常处理 区别异常的类型: Error与 Exception,必检异常与免检异常 在方法头中声明异常 在方法中抛出异常 o/用try- catch块处理异常 了解异常的传播 o在try- catch块中重新抛出异常 o在try- catch块中使用 finally.子句 o了解何时使用异常 o声明自定义异常类
理解异常和异常处理 区别异常的类型:Error与Exception,必检异常与免检异常 在方法头中声明异常 在方法中抛出异常 用try-catch块处理异常 了解异常的传播 在try-catch块中重新抛出异常 在try-catch块中使用finally子句 了解何时使用异常 声明自定义异常类 第6章 异常处理

程序错误:语法错误、运行错误、逻辑错误 浯法错误是指代码没有遵循语言的规则,可以由编 器检查发现。 o在程序运行过程中,如果环境发现一个不可能执行 的操作,就会出现运行错误。 o如果程序没有按照预期的方案执行,就会发生逻辑 错误
程序错误:语法错误、运行错误、逻辑错误 语法错误是指代码没有遵循语言的规则,可以由编 译器检查发现。 在程序运行过程中,如果环境发现一个不可能执行 的操作,就会出现运行错误。 如果程序没有按照预期的方案执行,就会发生逻辑 错误

Runtime errors import javax. swing. JOptionPane public class Test i public static void main(string[] args)i String input JOptionPane showInputDialog(null, nPlease enter an integer )i nt number Integer parseInt (input)i 如果这行出现异常,就会跳过 // Display the result 该方法中其余的行,程序终止 JOptionPane showMessageDialog(null, u The number entered is "+ number)i System. exit(o)i V程序结束
Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); System.exit(0); } } 如果这行出现异常,就会跳过 该方法 中其余的行,程序终止 程序结束

程序 readfile(自然语言描述) readFile open the file determine its size allocate the much memory read the file into memory close the file
程序readFile(自然语言描述) readFile{ open the file; determine its size; allocate the much memory; read the file into memory; close the file; }

传统错误处理:通过函数返回值 readFile initialize errorcode=0i open the filei the Fileopen) determine its size if(getTheFileLenght t allocate the much memory; if(getEnough Memory read the file into memory: (readFailed) errorCode=-1 } felse t error Code=-2: felse t errorCode=-3: close the file if(the FildDidntclose & errorCode==ort errorcode 4 felse t errorCode else t errorCode=-6: return errorCode
传统错误处理:通过函数返回值 readFile{ initialize errorCode=0; open the file; if(theFilesOpen){ determine its size; if(getTheFileLenght){ allocate the much memory; if(getEnoughMemory){ read the file into memory; if(readFailed){ errorCode=-1; } }else { errorCode=-2; } }else { errorCode=-3; } close the file; if(theFildDidntClose && errorCode==0){ errorCode=-4; }else { errorCode=-5; } } else { errorCode=-6; } return errorCode; }

异常处理机制 readFile open the file determine its size allocate the much memory read the file into memory close the file 3 catch(fileOpen Failed)t handl 1 catch (sizedetermination Failed)tt handle 3 catch(memory Allocation Failed)t handle: 3 catch (readFailed) handle 3 catch(fileCloseFailed)t handle
异常处理机制 readFile{ try{ open the file; determine its size; allocate the much memory; read the file into memory; close the file; } catch(fileOpenFailed){ handle; } catch(sizedeterminationFailed){ handle; } catch(memoryAllocationFailed){ handle; } catch(readFailed){ handle; } catch(fileCloseFailed){ handle; } }

Catch runtime errors import javax. swing. JOptionPane public class Test t public static void main(string[] args)i String input JOptionPane showInputDialog(null, "Please enter an integer") 如果这行出现异常,就会跳过try子句中其余 int number Integer parseInt(input) 的行,并把程序控制转移到 catch子句 // Display the result JOptionPane showMessageDialog(null ii The number entered is number catch( Excepti。nex) JOptionPane. showMes sageDialog(null iNcorrect input: an integer is required")i 捕获并处理这个异常后,程序控制转移到 Iry- catch块之后的第一条语句 System. out. println ("Execution continues ...")i System. exit(o)
Catch Runtime Errors import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { try { String input = JOptionPane.showInputDialog(null, "Please enter an integer"); int number = Integer.parseInt(input); // Display the result JOptionPane.showMessageDialog(null, "The number entered is " + number); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "Incorrect input: an integer is required"); } System.out.println("Execution continues ..."); System.exit(0); } } 如果这行出现异常,就会跳过try子句中其余 的行,并把程序控制转移到catch子句 捕获并处理这个异常后,程序控制转移到 Try-catch块之后的第一条语句

EXception Classes ClassNot Found Exception IOException ArithmeticException AWTException NullPointer Except RuntimeExe IndexOutOfBounds Exception Throuble Several more classes galArgumentException Linkage error Several more classes VirtualMachine Error Erro AWTError
Exception Classes LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException

System Errors ClassNot Found Exception IOExcepti Arithmetic Exception Exception AWTException lullPointer Excepti RuntimeException IndexOutofBounds Exception Object Throwal Several more classes IllegalArgument Exception System errors是由Java虚 Several more classes 拟机抛出并在Eror类中描 LinkageError 述的。Eror类描述内部的 VirtualMachine Error 系统错误。这种错误很少 Error 发生,一旦发生,除了通 AWTErro 知用户以及尽量稳妥地结 束程序外,几乎什么也不 Several more classes 能做
System Errors LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException System errors是由Java虚 拟机抛出并在Error类中描 述的。Error类描述内部的 系统错误。这种错误很少 发生,一旦发生,除了通 知用户以及尽量稳妥地结 束程序外,几乎什么也不 能做

Exceptions Exceptions类描述由程序 ClassNot Found Exception 和外部环境引起的错误 这些错误能通过程序 IOException 捕获和处理。 Arithmetic Exception ception AWTException INullPointer Except RuntimeException IndexOutOfBounds Exception hrowab Several more classes IllegalArgument Exception LinkageError Several more classes Virtualmachine erro Error AWTError Several more classes
Exceptions LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException Exceptions类描述由程序 和外部环境引起的错误 ,这些错误能通过程序 捕获和处理
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第1章 Java 语言入门.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第7章 图形用户界面程序设计入门(7.4)菜单、工具栏、对话框.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第7章 图形用户界面程序设计入门(7.3)创建图形用户界面.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第7章 图形用户界面程序设计入门(7.2)事件驱动程序.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第7章 图形用户界面程序设计入门(7.1)图形用户界面.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第1章 Java概述.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第2章 Java基本编程结构.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第3章 字符串和数组.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第4章 对象和类.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第5章 继承与多态.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第6章 异常处理.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第七章 图形编程.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第八章 Java Swing与事件处理.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第9章 Applet基础.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第10章 多线程.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第11章 输入输出流及文件.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第12章 Java的网络编程.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第2章 标识符、关键字和数据类型.ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第3章 运算符、表达式和语句.ppt
- 清华大学:《操作系统》课程教学资源(PPT课件)第七章 文件系统(向勇).ppt
- 北京大学计算机系:《Java 2实用教程》(第3版)PPT教学课件 第4章 对象和类.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第10章 JDBC链接数据库.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第11章 Java异常处理.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第12章 Java I/O处理.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)Java技术概览.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第1章 J ava开发工具及环境.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第2章 Java语言基础.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第3章 程序流程控制.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第4章 数组.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第5章 字符串操作.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第6章 容器类简介.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第7章 面向对象技术导论.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第8章 对象的初始化和清理.ppt
- 机械工业出版社:《Java完全自学手册》教材电子教案(PPT课件讲稿)第9章 多线程编程.ppt
- 清华大学:《数据结构》课程教学资源(练习答案)第一章答案.doc
- 清华大学:《数据结构》课程教学资源(练习答案)第十章答案.doc
- 清华大学:《数据结构》课程教学资源(练习答案)第二章答案.doc
- 清华大学:《数据结构》课程教学资源(练习答案)第三章答案.doc
- 清华大学:《数据结构》课程教学资源(练习答案)第四章答案.doc
- 清华大学:《数据结构》课程教学资源(练习答案)第五章答案.doc