tailieunhanh - Lecture Introduction to Java programming - Chapter 13: Exception handling
When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or terminate gracefully? This is the subject we will introduce in this chapter. | Chapter 13 Exception Handling Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or terminate gracefully? This is the subject we will introduce in this chapter. Objectives To get an overview of exceptions and exception handling (§). To explore the advantages of using exception handling (§). To distinguish exception types: Error (fatal) vs. Exception (nonfatal), and checked vs. unchecked (§). To declare exceptions in a method header (§). To throw exceptions in a method (§). To write a try-catch block to handle exceptions (§). To explain how an exception is propagated (§). To use the finally clause in a try-catch block (§). To use exceptions only for unexpected errors (§). To rethrow exceptions in a catch block (§). To create chained exceptions (§). To define custom exception classes (§). Exception-Handling Overview Quotient Run QuotientWithIf Run QuotientWithException Run Show runtime error Fix it using an if statement What if the runtime error occurs in a called method? Exception Advantages QuotientWithMethod Run Now you see the advantages of using exception handling. It enables a method to throw an exception to its caller. Without this capability, a method must handle the exception or terminate the program. Handling InputMismatchException InputMismatchExceptionDemo Run By handling InputMismatchException, your program will continuously read an input until it is correct. Exception Types System Errors 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 Exception describes errors caused by your program and external circumstances. These errors can be . | Chapter 13 Exception Handling Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or terminate gracefully? This is the subject we will introduce in this chapter. Objectives To get an overview of exceptions and exception handling (§). To explore the advantages of using exception handling (§). To distinguish exception types: Error (fatal) vs. Exception (nonfatal), and checked vs. unchecked (§). To declare exceptions in a method header (§). To throw exceptions in a method (§). To write a try-catch block to handle exceptions (§). To explain how an exception is propagated (§). To use the finally clause in a try-catch block (§). To use exceptions only for unexpected errors (§). To rethrow exceptions in a catch block (§). To create chained exceptions (§). To define custom exception classes (§). .
đang nạp các trang xem trước