复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 15 Exceptions and Assertions

Chapter 15 Exceptions and assertions Prerequisites for part Iv Chapter 8 Inheritance and Polymorphism Chapter 15 Exceptions and assertions Chapter 16 Simple Input and Output Nothing is impossible Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Prerequisites for Part IV Chapter 8 Inheritance and Polymorphism Chapter 16 Simple Input and Output Chapter 15 Exceptions and Assertions Nothing is impossible

Objectives To know what is exception and what is exception handling (§152) To distinguish exception types Error(fatal)vs. Exception(non- fatal), and checked VS. uncheck exceptions(8 15.2) To declare exceptions in the method header($ 15.3) ● To throw exceptions out of a method(§15.3 o To write a try-catch block to handle exceptions($ 15.3 o To explain how an exception is propagated(8 15. 3) o To rethrow exceptions in a try-catch block(8 15.4) o To use the finally clause in a try-catch block($ 15.5) ● To know when to use exceptions(§156) o to declare custom exception classes(8 15.7 Optional) tional o To apply assertions to help ensure program correctness(8 15.8) Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To know what is exception and what is exception handling (§15.2). ⚫ To distinguish exception types: Error (fatal) vs. Exception (nonfatal), and checked vs. uncheck exceptions (§15.2). ⚫ To declare exceptions in the method header (§15.3). ⚫ To throw exceptions out of a method (§15.3). ⚫ To write a try-catch block to handle exceptions (§15.3). ⚫ To explain how an exception is propagated (§15.3). ⚫ To rethrow exceptions in a try-catch block (§15.4). ⚫ To use the finally clause in a try-catch block (§15.5). ⚫ To know when to use exceptions (§15.6). ⚫ To declare custom exception classes (§15.7 Optional). ⚫ To apply assertions to help ensure program correctness (§15.8)

Syntax Errors Runtime Errors, and Logic errors there are three categories of errors: syntax errors runtime errors, and logic errors o Syntax errors arise because the rules of the language have not been followed They are detected by the compiler o Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out o Logic errors occur when a program doesn 't perform the way it was intended to Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Syntax Errors, Runtime Errors, and Logic Errors there are three categories of errors: syntax errors, runtime errors, and logic errors. ⚫Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. ⚫Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. ⚫ Logic errors occur when a program doesn't perform the way it was intended to

Runtime errors mport javax. swing. vOptionpane; public class Test public static void (String [] args)t String input JoptionPane show InputDialog(null Please enter an integer) int number nteger parse If an exception occurs on this line. the rest lines in the method / Display the result are skipped and the program is terminated JOptionPane showMessageDialog(null The number entered is+ number) System. exit(0) V Terminated duction to Java
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 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); } } If an exception occurs on this line, the rest lines in the method are skipped and the program is terminated. Terminated

Catch Runtime errors mport javax. swing. JOptionPane; ublic class test public static void main(String[] args) try i stril t= JOptionPane. showInput Dialog(null Please enter an int number Integer parseInt(input)i If an exception occurs on this line, the rest lines in the try clause are skipped and the control is transferred to the catch clause // Display the result JOptionPane showMessageDialog(null The number entered is+ number JOptionPane showMessageDialog(null Incorrect input: an integer is required")i After the exception is caught and processed, the control is transferred to the next statement after the try-catch block System. out. println(" Execution continues System. exit(0) duction to Java
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 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); } } If an exception occurs on this line, the rest lines in the try clause are skipped and the control is transferred to the catch clause. After the exception is caught and processed, the control is transferred to the next statement after the try-catch block

Exception Classes ClassNotFoundException IOEXception Arithmetic Excepti Exception AWTException HNullPointer Exception] RuntimeException IndexOutofBoundsException Object Throwab Several more classes IllegalArgument Exception Linkage error Several more classes Ⅴ irtualMachine Error Error AWTError Several more classes Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 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 are thrown LinkageError Several more classes by/VM and represented in the error class The error VirtualMachine Error class describes internal Error system errors. Such errors AWTErro rarely occur. If one does there is little you can do Several more classes beyond notifying the user and trying to terminate the program gracefully Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 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 are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully

Exceptions Exceptions are represented ClassNot Found Exception in the exception class that describes errors caused by IOException our program and external Arithmetic Exception circumstances These errors Exception AWTException can be caught and handled by INullPointer Except your program RuntimeException IndexOutOfBounds Exception hrowab Several more classes IllegalArgument Exception LinkageError Several more classes Virtualmachine erro E rror AWTError Several more classes Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 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 are represented in the Exception class that describes errors caused by your program and external circumstances. These errors can be caught and handled by your program

Runtime exceptions ClassNotFound Exception IOException Arithmetic Exception Exception AWTException NullPointerException RuntimeException IndexOutofBounds Exception Object Throwable Several more classes IllegalArgument Exception Linkage error Several VirtualMachine Error Error Runtime exceptions are AWTError represented in the RuntimeException class that Several more classes describes programming errors, such as bad casting access ing an out-of- bounds array, and numeric errors Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Runtime 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 Runtime exceptions are represented in the RuntimeException class that describes programming errors, such as bad casting, accessing an out-ofbounds array, and numeric errors

Checked Exceptions Vs Unchecked eD Exceptions ORuntimeException, Error and their subclasses are known as unchecked exceptions oAll other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Checked Exceptions vs. Unchecked Exceptions ⚫RuntimeException, Error and their subclasses are known as unchecked exceptions. ⚫All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 14 Applets, Images, and Audio.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 13 Creating User Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 12 Event-Driven Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 11 Getting Started with GUI Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 10 Object-Oriented Modeling(oom).ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 9 Abstract Classes and Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 8 Inheritance and Polymorphism.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 7 String.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 6 Objects and Classes.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 4 Methods.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 3 Control Statements.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 2 Primitive Data Types and Operations.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 1 Introduction to Computers, Programs, and Java.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 0 course intro Programming Language(Using Java).ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life_题目要求.pdf
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)验证哥德巴赫猜想.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Ansi Temple.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)富人Smith的生日 If else Switch.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 16 Simple Input and Output.ppt
- 复旦大学:《信息安全》教学课件_01 Classical Encryption Techniques.pdf
- 复旦大学:《信息安全》教学课件_02 Classical Encryption Techniques(cont.).pdf
- 复旦大学:《信息安全》教学课件_03 Modern Block Ciphers.pdf
- 复旦大学:《信息安全》教学课件_04 Public Key Cryptography, RSA.pdf
- 复旦大学:《信息安全》教学课件_05 Message authentication and Hash function.pdf
- 复旦大学:《信息安全》教学课件_06 The Intro to Information Security.pdf
- 复旦大学:《信息安全》教学课件_07-08 Public Key Infrastructure(PKI)公钥基础设施——公钥技术的应用.pdf
- 复旦大学:《信息安全》教学课件_09 Authentication and supplements.pdf
- 复旦大学:《信息安全》教学课件_10 Authentication Kerberos.pdf
- 复旦大学:《信息安全》教学课件_11.1 IP Security.pdf
- 复旦大学:《信息安全》教学课件_11.2 Web & EC Security.pdf
- 复旦大学:《信息安全》教学课件_12-13 Software Security.pdf
- 复旦大学:《信息安全》教学课件_14 防火墙 Firewall.pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第1章 信息系统安全概述、第2章 密码学概论(1/3).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第2章 密码学概论(2/3).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第3章 现代加密算法(2/3).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第3章 现代加密算法(2/4).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第3章 现代加密算法(3/4).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第3章 现代加密算法(4/4)、第4章 密码应用(1/4).pdf