Exception Handling 异常处理 Institute of Computer Software 2022-2-27 Nanjing University
异常处理 Exception Handling 2022-2-27 Institute of Computer Software Nanjing University 1
&雪扇 摘要 UNIVE 2 口简介 口Java异常处理 口Java Exception机制的不当使用示例 Effective Java Exceptions 口Eiffel异常处理 口小结 Institute of Computer Software 2022-2-27 Nanjing University
摘要 简介 Java 异常处理 Java Exception机制的不当使用示例 Effective Java Exceptions Eiffel 异常处理 小结 2022-2-27 Institute of Computer Software Nanjing University 2
&雪扇 回顾DbC UNIVE DbC vs Defensive Programming 口DP中产生异常的语句是程序体本身的组成部分 口DbC中Precondition:还是程序文档的组成部分 Check our DbC demo... Institute of Computer Software 2022-2-27 Nanjing University
回顾DbC DbC vs Defensive Programming DP中产生异常的语句是程序体本身的组成部分 DbC中Precondition还是程序文档的组成部分 Check our DbC demo… 2022-2-27 Institute of Computer Software Nanjing University 3
&扇 002 简介 UNIVE 口问题 口何谓“异常(Exception)”? ■一种情况要“异常”到什么程度才算“异常”? 口为什么要引入异常处理机制? Robustness?Readability? 口如何进行异常处理? Mandatory or Optional 口不同的认识,不同的答案 ▣Java/C++/C# Eiffel Institute of Computer Software 2022-2-27 Nanjing University
简介 问题 何谓“异常(Exception)”? 一种情况要“异常” 到什么程度才算“异常”? 为什么要引入异常处理机制? Robustness? Readability? 如何进行异常处理? Mandatory or Optional 不同的认识,不同的答案 Java/C++/C# Eiffel 2022-2-27 Institute of Computer Software Nanjing University 4
&扇 Java Exception Mechanisms 102 口Java对Exception的界定较宽 口因而需认真区分不同类型的Exceptions Java的Exception机制回顾 try/catch/finally throw Exceptions 口自定义Exceptions Java Exception Design by Contract 口Exception的转换 Institute of Computer Software 2022-2-27 Nanjing University
Java Exception Mechanisms Java 对 Exception 的界定 较宽 因而 需 认真区分 不同类型的 Exceptions Java的Exception机制回顾 try/catch/finally throw Exceptions 自定义Exceptions Java Exception与Design by Contract Exception的转换 2022-2-27 Institute of Computer Software Nanjing University 5
&扇 Understanding Java Exceptions UNIVE 6 这部分使用Manchester University,Kung-Kiu Lau, CS31O1的PPT Slides Institute of Computer Software 2022-2-27 Nanjing University
Understanding Java Exceptions 这部分使用Manchester University, Kung-Kiu Lau, CS3101 的PPT Slides 2022-2-27 Institute of Computer Software Nanjing University 6
What are Exceptions? Many "exceptional"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
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 elf (i.e.violations of the contra would expect these things to system errors-Problems happen in prod tion code,and so neer These are outside our contror. 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 hierawe woua normally check for just crash the program n a rather confthese.vand 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 个 Error Exception RuntimeException Exception- The RuntimeException World Checked RuntimeException Exceptions Unchecked Program Er工Or 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