Types of exceptions in Java

on waitingforcode.com

Types of exceptions

You're still doing Java/C#/JavaScript/Python/PHP... and need a wind of change? I was like that 4 years ago. I changed then to the data engineering field and it solved my existential problems :) If you want to follow my path, I prepared a course that will help you with that! Join the class!
Using of standard Java exceptions simplifies the understanding of every project. But they are much exceptions that we can use and it's not always simple to found the right one.

In this article we'll focus on different types of exceptions in Java. To simplify the reading, we won't divide this article on different parts. Instead of it, all exceptions will be presented in two tables : one for unchecked exceptions and another one for checked exceptions. The difference between these two types is that ones are can be detected at conception time and the others only at runtime.

Exceptions detected at conception time are called checked ant they must be caught or thrown. They also can be predicted, for example: when we try to work with a non-existent file with FileInputStream, an FileNotFoundException can be thrown. This type of exceptions is declared at API level.

Unchecked exceptions are more difficult to predict and occur at runtime, for example because of unexpected input as the operations on null object while it was expected to be not null.

Types of checked exceptions

See checked exceptions in Java

Unchecked exceptions

See unchecked exceptions in Java

We saw that several exceptions exist in Java to handle different strange scenarios. We can use IllegalThreadStateException to handle a Thread's bad state or IllegalAccessException on trying to access to a non-visible method or field. You should use these exceptions to facilitate the understanding of your code. Is better to see explicitly that in some method only restricted values of arguments are accepted at development time, rather than discover a bug in runtime environment without knowing its origine.

Share on: