《JAVA语言程序设计》第6章.pptVIP

  • 5
  • 0
  • 约7.35千字
  • 约 21页
  • 2017-08-19 发布于江西
  • 举报
《JAVA语言程序设计》第6章.ppt

* 软件程序肯定会发生错误/问题 what really matters is what happens after the error occurs. How is the error handled? Who handles it? Can the program recover, or should it just die? 从外部问题(硬盘、网络故障等)到编程错误(数组越界、引用空对象等) Java语言利用异常来使程序获得处理错误的能力(error-handling) 异常事件(Exceptional Event) An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. 异常(Exception) * 当一个Java程序的方法产生一个错误,该方法创造一个异常对象并将其交给运行系统 In Java terminology, creating an exception object and handing it to the runtime system is called throwing an exception(抛出异常) 运行系统从错误发生处开始寻找处理错误的程序段 The exception handler chosen is said to catch the exception(捕获异常) 捕获异常的过程可以沿方法调用的逆向顺序寻找 异常(Exception) * 为什么采用异常(Exception) 隔绝正常代码和错误处理代码 异常(Exception) readFile { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } 潜在的问题: What happens if the file cant be opened? What happens if the length of the file cant be determined? What happens if enough memory cant be allocated? What happens if the read fails? What happens if the file cant be closed? * readFile { try { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } catch (fileOpenFailed) { doSomething; } catch (sizeDeterminationFailed) { doSomething; } catch (memoryAllocationFailed) { doSomething; } catch (readFailed) { doSomething; } catch (fileCloseFailed) { doSomething; } } 潜在的问题: What happens if the file cant be opened? What happens if the length of the file cant be determined? What happens if enough memory cant be allocated? What happens if the read fails? What happens if the file cant be closed? * 为什么采用异常(Exception) 异常(Exception) method1 { call method2; } method2 { call method3; } method3 { call readFile; } method1 { errorCodeType error; error = call method2; if (error) d

文档评论(0)

1亿VIP精品文档

相关文档