TAILIEUCHUNG - Lecture An Introduction to Object-Oriented Programming with Java (4/e): Chapter 8 - C. Thomas Wu

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 = (null, "Age:"); age = (inputStr); : ten at () at () at () 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

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.