Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Chapter 8 - Exceptions and assertions. Program correctness guarantees correct results for all valid input. But what happens when the input is invalid? Another important criterion of program reliability is the robustness, which measures how well the program runs under various conditions. In this chapter, we will describe how to code this exception-handling mechanism in Java to improve the program’s robustness. | Chapter 8 Exceptions and Assertions 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Objectives After you have read and studied this chapter, you should be able to Improve the reliability of code by incorporating exception-handling and assertion mechanisms. Write methods that propagate exceptions. Implement the try-catch blocks for catching and handling exceptions. Write programmer-defined exception classes. Distinguish the checked and unchecked, or runtime, exceptions. 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definition An exception represents an error condition that can occur during the normal course of program execution. When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught. 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. We can increase our programs’ reliability and robustness if we catch the exceptions ourselves using error recovery routines we develop. One way to do this is to wrap the statements that may throw an exception with the try-catch control statement. Not Catching Exceptions String inputStr; int age; inputStr = JOptionPane.showInputDialog(null, "Age:"); age = Integer.parseInt(inputStr); java.lang.NumberFormatException: ten at java.lang.Integer.parseInt(Integer.java:405) at java.lang.Integer.parseInt(Integer.java:454) at Ch8Sample1.main(Ch8Sample1.java:20) Error message for invalid input 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Consider the given example. What would happen if the user enters a value such as the text 'ten' instead of 10? The parseInt method cannot convert such an input to an internal numerical format. This type of error is called an exception, and it will result in displaying an error | Chapter 8 Exceptions and Assertions 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Objectives After you have read and studied this chapter, you should be able to Improve the reliability of code by incorporating exception-handling and assertion mechanisms. Write methods that propagate exceptions. Implement the try-catch blocks for catching and handling exceptions. Write programmer-defined exception classes. Distinguish the checked and unchecked, or runtime, exceptions. 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definition An exception represents an error condition that can occur during the normal course of program execution. When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught. 4th Ed Chapter 8 - ©The McGraw-Hill Companies, Inc. Permission required