[8.1] Differentiate among checked exceptions, unchecked exceptions, and Errors
The Java compiler and its runtime treat the exception categories in a different manner. This implies that separate rules exist to define methods that throw exceptions and code that handles them.
In this section, you’ll learn about the categories of exceptions in Java: checked exceptions, runtime exceptions, and errors.
As depicted in figure 7.8, exceptions can be divided into three main categories:
Runtime exceptions and errors are collectively referred to as unchecked exceptions.
Of these three types, checked exceptions require most of your attention when it comes to coding and using methods. Runtime exceptions represent programming errors. Checks should be inserted to prevent runtime exceptions from being thrown. There are few options you can use for the errors, because they’re thrown by the JVM.
For the OCA Java SE 8 Programmer I exam, it’s important to have a crystal-clear understanding of these three categories of exceptions, including their similarities and differences.
Exception categories are related to each other; all extend the class java.lang.Throwable (as shown in the class hierarchy in figure 7.9).
Here’s the categorization of exceptions based on their class hierarchy:
Let’s examine each of these categories in detail.
When we talk about handling exceptions, checked exceptions take up most of our attention.
What is a checked exception?
In this exam, you may have to select which type of reference variable to use to store the object of the thrown checked exception in a handler. To answer such questions correctly, remember that a checked exception subclasses java.lang.Exception but not java.lang.RuntimeException.
A checked exception is part of the API and is well documented. For a quick example, here’s the declaration of the constructor of the class java.io.FileInputStream in the Java API:
public FileInputStream(File file) throws FileNotFoundException
Checked exceptions are unacceptable conditions that a programmer foresees at the time of writing a method. By declaring these exceptions as checked exceptions, the author of the method makes its users aware of the exceptional conditions that can arise from its use. The user of a method with a checked exception must handle the exceptional condition accordingly.
Although you’ll spend most of your time and energy combating checked exceptions, the runtime exceptions will give you the most headaches. This is particularly true when you’re preparing to work on real-life projects. Some examples of runtime exceptions are NullPointerException (the most common one), ArrayIndexOutOfBounds-Exception, and ClassCastException.
What is a runtime exception?
Together, runtime exceptions and errors are referred to as unchecked exceptions.
Whether you’re preparing for this exam or your real-life projects, you need to know when the JVM throws errors. These errors are considered to be serious exceptional conditions and they can’t be directly controlled by your code.
What is an error?
Let’s move on to creating methods that throw exceptions.
18.222.93.132