1. Errors and Exceptions
Now that we know how to handle exceptions, it is time to take a look at exception types.
2. Exceptions vs Errors
Not all problems or issues in Java are something that our applications can `try` and `catch`. Notice the word error was not used when talking about exceptions in Java. Java differentiates between `Error` and `Exception`. Errors are a serious problem in Java, such as running out of memory. They cannot be recovered from. `Exception`s are recoverable or avoidable issues, caused by incorrect logic or unexpected input, such as accessing an array index that doesn't exist.
3. Example Error
While we can't effectively handle an `Error`, we see their output. The application here keeps adding to an `ArrayList` until memory is exhausted and eventually results in the `OutOfMemoryError`.
4. Two types of exceptions
There are two classifications of exceptions in Java: checked and runtime exceptions. Runtime exceptions are also known as unchecked.
The major difference between checked and runtime exceptions is that we must handle checked exceptions by using try/catch or another means called throws which we'll see later. Runtime or unchecked exceptions do not require we handle them.
Checked exceptions usually occur around issues which we can anticipate but cannot always make sure do not happen. For example, an application may use a file on the computer. The fact that the file may have been deleted by the user is something we can anticipate, but isn't something we control. Java helps us by requiring that we check and handle these issues. By anticipating these issues, we can often recover from the issue and avoid the program crashing.
Runtime exceptions, on the other hand, are issues that we often cause through coding mistakes. Runtime exceptions can generally be avoided if we code carefully. Java does not require we handle these with try/catch or other means. Trying to access an array outside of its index causing an `IndexOutOfBoundsException` is a good example of a Runtime exception. We can, optionally, try to handle runtime exceptions, but when a runtime exception occurs, it is often difficult for the application to recover so its best if we avoid these exceptions through good programming.
5. Checked and Runtime exceptions
There are many different Runtime and Checked `Exception` types in Java.
`Exception` is the superclass for all checked exceptions that must be handled. When not handled, the Java compiler will fail. `FileNotFound` is a good example of a checked exception.
`RuntimeException` is a subclass of `Exception` and represents issues often of our own making, and do not require handling.
`IndexOutOfBoundsException` and `ArithmeticException` are good examples of `RuntimeExceptions`.
6. RuntimeException
Here are a few `RuntimeException` subclasses. Remember, we don't need a try-catch for these exceptions - they are unchecked. Generally, these can be avoided with good programming.
7. "Checked" Exception
All `Exception` that are not subclass of `RuntimeException` are checked exceptions. There aren't many checked exceptions in the `java.lang` package. There is one example we can see.
Java has a way to load classes. This is typically done for things like databases where a special class called a driver is used to access the database. The way to load a class dynamically is with `Class.forName()`. This method requires that we catch `ClassNotFoundException`. If we don't, our code won't compile or run and we get this compile error.
8. Let's practice!
Let's go see some errors and exceptions.