课程: Java Exception Handling

What are throwables?

- [Instructor] In this course we will learn the definitions of throwable, error, and exception and why we need to learn how to handle them. No previous knowledge around these terms are needed. We will be using a Spring Boot app with a REST API to illustrate examples of throwables, errors, and exceptions throughout this course. Please refer to other courses on LinkedIn Learning on REST APIs and Spring Boot if you do not already have experience with these concepts. This includes understanding how to make a REST API with Spring, as well as how to make HTTP requests. An error is an object that can be thrown by an application to indicate a serious problem that should not be caught. Errors, often indicate abnormal conditions that should never occur. For that reason, a method does not have to declare that it could throw an error object. Later in chapter one, we will see an example called the stack overflow error. Exceptions are another object that can be thrown and indicates conditions that an application might want to catch and handle. There are two subclasses to the exception object; checked exceptions and unchecked exceptions or runtime exceptions. We will explore checked exceptions in chapter two, and in chapter three we will discuss runtime exceptions a little more. Lastly, both errors and exceptions are subclasses of the throwable class. The throwable class is a superclass of all errors and exceptions in Java. These objects can be thrown using the throw statement and they can be caught using the catch clause. Throwables, errors, and exceptions are indicators that an application is not performing as expected. Not handling errors or exceptions can lead to unpredictable behavior of your app and unreliability. For instance, some errors will make your app non-responsive and may even shut the application down. Beyond knowing that errors and exceptions are throwables, we also need to define unchecked and checked throwables. Note, this is different from checked and unchecked exceptions as mentioned before. All checked exceptions are checked throwables in Java. They are checked at compile time. This means any checked exceptions must be declared in the method hitter, caught, or handled in some way. If a checked exception could potentially occur in a method or a class and that exception is not handled, the code will not compile at all. Apps can potentially recover from checked throwables and continue its work. Unchecked throwables include all errors and any runtime exceptions, which are not checked at compile time. Apps will likely not be able to recover at all from an unchecked throwable. We now know what throwables, errors, and exceptions are and how they relate to each other. Understanding this will prove helpful in learning how to handle throwables, errors, and exceptions within your app.

内容