TAILIEUCHUNG - Lecture An introduction to object-oriented programming with Java: Chapter 8 - C. Thomas Wu

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. | ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8- Chapter 8 Exceptions and Assertions Animated Version Introduction to OOP with Java 4th Ed, C. Thomas Wu © The McGraw-Hill Companies, Inc. 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. Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 - 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. Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. 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. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 - Not Catching Exceptions Scanner scanner = new Scanner(); (“Enter integer:"); int number = (); Exception in thread “main” at () at () at () at () at () Error message for invalid input Intro to OOP with Java, C. Thomas Wu

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.