Exception Handling 异常处理 Institute of Computer Software 2021/2/9 Nanjing University
异常处理 Exception Handling 2021/2/9 Institute of Computer Software Nanjing University 1
R病 这摘要 口简介 口Java异常处理 口 Java Exception机制的不当使用示例 n Effective Java Exceptions 口 Eiffel异常处理 小结 nstitute of Computer Software 2021/2/9 Nanjing University
摘要 简介 Java 异常处理 Java Exception机制的不当使用示例 Effective Java Exceptions Eiffel 异常处理 小结 2021/2/9 Institute of Computer Software Nanjing University 2
R病 回顾DbC a DbC vs Defensive Programming 口DP中产生异常的语句是程序体本身的组成部分 口DbC中 Precondition还是程序文档的组成部分 a Check our dbc demo nstitute of Computer Software 2021/2/9 Nanjing University
回顾DbC DbC vs Defensive Programming DP中产生异常的语句是程序体本身的组成部分 DbC中Precondition还是程序文档的组成部分 Check our DbC demo… 2021/2/9 Institute of Computer Software Nanjing University 3
R病 简介 口问题 口何谓“异常( Exception) 种情况要“异常”到什么程度才算“异常 口为什么要引入异常处理机制? Robustness? Readability 口如何进行异常处理? Mandatory or Optiona 口不同的认识,不同的答案 口Java/C++/C# o Eiffe nstitute of Computer Software 2021/2/9 Nanjing University
简介 问题 何谓“异常(Exception)”? ◼ 一种情况要“异常”到什么程度才算“异常”? 为什么要引入异常处理机制? ◼ Robustness? Readability? 如何进行异常处理? ◼ Mandatory or Optional 不同的认识,不同的答案 Java/C++/C# Eiffel 2021/2/9 Institute of Computer Software Nanjing University 4
R病 A Java Exception Mechanisms 5 口Java对 Exception的界定较宽 口因而需认真区分不同类型的 Exceptions Java的 Exception机制回顾 a try/ catch/find a throw Exceptions 口自定义 Exceptions 口 Java Exception与 Design by Contract 口 Exception的转换 nstitute of Computer Software 2021/2/9 Nanjing University
Java Exception Mechanisms Java 对 Exception 的界定 较宽 因而 需 认真区分 不同类型的 Exceptions Java的Exception机制回顾 try/catch/finally throw Exceptions 自定义Exceptions Java Exception与Design by Contract Exception的转换 2021/2/9 Institute of Computer Software Nanjing University 5
R病 f Understanding Java Exceptions 6 口这部分使用 Manchester University, Kung- Kiu lau CS3101的 PPT SLides nstitute of Computer Software 2021/2/9 Nanjing University
Understanding Java Exceptions 这部分使用Manchester University, Kung-Kiu Lau, CS3101 的PPT Slides 2021/2/9 Institute of Computer Software Nanjing University 6
What are EXceptions? Manyexceptional things can happen during the running of a program, e.g User mis-types input checked Web page not available File not found Array index out of bounds unchecked Method called on a null object Divide by zero Out of memory sys errors Bug in the actual language implementation EXceptions are unexpected conditions in programs
7 What are Exceptions? Many “exceptional” things can happen during the running of a program, e.g.: • Bug in the actual language implementation • User mis-types input • Web page not available • File not found • Divide by zero • Array index out of bounds • Method called on a null object • Out of memory Exceptions are unexpected conditions in programs. checked unchecked sys errors
We can distinguish 3 categories checked exceptions- Problems to do with the program's interaction with"the world unchecked exceptions The world is unpredictable, so we itself(i.e. violations of the d would expect these things to system errors- Problems happen in proa ation code, and so nee These are outside our contron These should be removed by testing and not occur in production code The World Checked EXceptions Unchecked Program Exceptions System System Errors 8
8 We can distinguish 3 categories: • checked exceptions — Problems to do with the program's interaction with “the world”. • unchecked exceptions — Problems within the program itself (i.e. violations of the contract, or bugs). • system errors — Problems with the underlying system. These are outside our control. The world is unpredictable, so we would expect these things to happen in production code, and so need to handle them. These should be removed by testing, and not occur in production code. Unchecked Exceptions System Errors Program System The World Checked Exceptions
Checked vs Unchecked Exceptions an important distinction which the Java it's normal to let these pn class hijerawe woe normay theck for just crash the program h a rather conthese, and deal with them so we can debug it when they occur The World Checked Exceptions Unchecked Program EXceptions System System Errors Exception handling is the business of handling these things appropriately 9
9 Checked vs Unchecked Exceptions Unchecked Exceptions System Errors Program System The World Checked Exceptions an important distinction, which the Java Exception class hierarchy does make, but in a rather confusing way we would normally check for these, and deal with them when they occur. it's normal to let these just crash the program so we can debug it. Exception handling is the business of handling these things appropriately
EXception Hierarchy in Java Throwable Exception RuntimeExcep七ion Excep The Run七 imeException World Checked RuntimeException EXceptions Unchecked Program E卫。 EXceptions System System Errors 10
10 Exception Hierarchy in Java Throwable Exception RuntimeException Error . . . Unchecked Exceptions System Errors Program System The World Checked Exceptions Error RuntimeException Exception − RuntimeException