Exception Handling in Java-2
In the first article, we have discussed the definition and default exception handling in java. If you haven’t read that post please read that one first.
In this article, we will understand the classification of exceptions in detail.
Recall the example in the first article, before leaving for a trip your girlfriend reminds you to take a driving license. She asked you to check for license keeping in mind that a police officer may ask anytime during the trip. If you don't find the license you should decide on an alternative way before starting the trip keeping in mind that an unexpected situation may occur.
Checked Exceptions:
In these types of exceptions, the compiler will make sure whether the programmer has written exception handling code or not in the compile-time itself. This helps in the smooth execution of the program in run time. That's the reason these exceptions are called compile-time exceptions as handling of these exceptions occurs at compile time not because these exceptions occur at compile time. Read that line again 🤌.
In our fantasy example, LicenseMissingException is a checked exception.
The compiler will yell at us saying you are asking me to write on the output.txt file, but at run time there may be a chance that the output.txt file may not be available (possibility of FileNotFoundException ). If FileNotFoundException occurs how you are going to handle it. So compiler will compile the code if and only if we handle this exception before runtime. A possible compile error will be in this format.
javac /tmp/PGmraqCkod/CheckedExceptionDemo.java
/tmp/PGmraqCkod/CheckedExceptionDemo.java:5: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
The above error doesn’t mean an exception has occurred. It says there is a possibility of that occurrence.
Come back to our fantasy example, Will you ever think of what to be done if you meet with an accident during the trip. Obviously NO! These kinds of unexpected events happen during runtime. We can’t plan alternatives before starting the trip for such kinds of events.
Unchecked Exceptions:
The exceptions which are not checked by the compiler whether the programmer had written handling code or not are called unchecked exceptions. Since checking for the handling code is not done compile-time, these are called runtime exceptions.
Note: Runtime Exception and its child classes, Errors and its child classes are unchecked exceptions and all the remaining exceptions are considered as checked exceptions (Refer to Exception hierarchy map).
Note: Whether exception is checked or unchecked they occur only at runtime. In case of checked handling code is checked at compile time. That doesn't mean they occur at compile time.
Refer to this image to understand partially checked and fully checked exceptions.
- A checked exception is said to be fully checked if and only if all its child classes are also checked. Ex: IOException and InterruptedException.
- A checked exception is said to be partially checked if at least one of its child classes is not checked. Ex: Exception and Throwable are the only possible partially checked exceptions.
We will end this with a small quiz. Try to identify the type for the following exceptions.
1. RuntimeException
2. Error
3. IOException
4. Exception
5. InterruptedException
6. Throwable
7. ArithmeticException
8. NullPointerException
9. FileNotFoundException
All the best ❤️🔥. We will discuss Customized Exception Handling using the try-catch-finally block in the next post.
Clap and Share 😁. You can connect with me on LinkedIn, Twitter, and Instagram.