Lecture Introduction to programming with Java - Chapter 14: Exception handling

This chapter presents the following content: Using try and catch blocks to handle "Dangerous" method calls, numberformatexception, line plot example, try and catch blocks - more details, two types of exceptions - checked and unchecked, unchecked exceptions, checked exceptions,.and other contents. | Chapter 14 - Exception Handling Using try and catch Blocks to Handle "Dangerous" Method Calls NumberFormatException Line Plot Example try and catch Blocks - More Details Two Types of Exceptions - Checked and Unchecked Unchecked Exceptions Checked Exceptions Using API Documentation when Writing Exception-Handling Code When a try Block Throws Different Types of Exceptions The Exception Class and its getMessage Method Multiple catch blocks Understanding Exception Messages 1 Using try and catch Blocks to Handle "Dangerous" Method Calls Some API method calls are "dangerous" in that they might possibly lead to a runtime error. Example of a "safe" API method call (no runtime error possible): () Example of an API method call that might lead to a runtime error: () Technique for handling such runtime errors: Use exception handling. More specifically, surround the method call with a try block and insert a catch block immediately after the try block. 2 Using try and catch Blocks to Handle "Dangerous" Method Calls Syntax for try and catch blocks: try { } catch ( ) { } Example try and catch code fragment: try { quantity = (userEntry); } catch (NumberFormatException nfe) { ("Invalid quantity entered." + " Must be a whole number."); } Normally, one or more of these statements will be a "dangerous" API method call or constructor call. The exception class should match the type of exception that the try block might throw. 3 Using try and catch Blocks to Handle "Dangerous" Method Calls Semantics for previous slide's try and catch code fragment: If the userEntry string contains all digits, then: The int version of userEntry is assigned into quantity. The JVM skips the catch block and continues below it. If the userEntry string does not contain all digits, then: The parseInt method throws a NumberFormatException object. The | Chapter 14 - Exception Handling Using try and catch Blocks to Handle "Dangerous" Method Calls NumberFormatException Line Plot Example try and catch Blocks - More Details Two Types of Exceptions - Checked and Unchecked Unchecked Exceptions Checked Exceptions Using API Documentation when Writing Exception-Handling Code When a try Block Throws Different Types of Exceptions The Exception Class and its getMessage Method Multiple catch blocks Understanding Exception Messages 1 Using try and catch Blocks to Handle "Dangerous" Method Calls Some API method calls are "dangerous" in that they might possibly lead to a runtime error. Example of a "safe" API method call (no runtime error possible): () Example of an API method call that might lead to a runtime error: () Technique for handling such runtime errors: Use exception handling. More specifically, surround the method call with a try block and insert a catch block immediately after the

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã 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.