Checked and Unchecked Exceptions in Java

Justgiveacar
2 min readJul 6, 2021

Hi there !

I’m recently reading some books and preparing for the OCA certification assessment and I want to share some interesting parts that are always omitted by us.

In Java, there are two types of exceptions:

Checked Exception

They are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

Invalid external conditions that cannot be directly controlled by the program.

  • FileNotFoundException: Thrown programmatically when code tries to reference a file that does not exist.
  • IOException: Thrown programmatically when there’s problem reading or writing a file.

Unchecked Exception

They are the exceptions that are not checked at compile time. It is up to the programmers to be civilized, and specify or catch the exceptions.

Defects or logical errors of the program, and cannot be recovered at Runtime.

  • ArithmeticException: Trying to divide an int by zero gives an undefined result. When this occurs, the JVM will throw an ArithmeticException.
  • ArrayIndexOutOfBoundsException: You know that array indexes start with 0 and go up to 1 less than the length of the array. If you try to get an element by an illegal index (-1 for example), an exception will be thrown.
  • ClassCastException: hrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. Java tries to protect you from impossible casts.
  • IllegalArgumentException: Thrown to indicate that a method has been passed an illegal or inappropriate argument. IllegalArgumentException is a way for your program to protect itself.
  • NullPointerException: Instance variables and methods must be called on a non-null reference. If the reference is null, the JVM will throw a NullPointerException.

--

--