checked and unchecked exceptions in java

What is the difference between checked and unchecked exceptions in java? This is another famous interview question.

Well!! before looking into checked and unchecked exceptions we need to look into Exception class itself.

What is an Exception

The java documentation gives a definition to Exception.

Exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

When ever an exception happens, java creates an object called Exception Object and it contains information about the event like when and where it is triggered from.

From Java perspective Exception is a class extended from class Throwable. The class hierarchy is given below.

exception-hierchey-java

Ok, Now we can discuss about checked exceptions and unchecked exceptions.

Checked Exception

Checked Exceptions are checked at compile time. They are extended from java.lang.Exception. There are two ways to handle a checked exception,

  • method should specify that it throws this particular exception on the method signature using throws keyword.
  • handle that exception using try… catch block

If a checked exception is not handled in either way said above, then it gives a compilation error. That is why it is said that checked Exceptions are compile time errors.

Creating Checked Exceptions

Ok, Lets try to create a Checked Exception. An example is given below.

Now we can create a Test class for this MyCheckedException.

As you can see the above program will not compile. To compile these we need to do either one of two options we said before.
1. Use trows keyword with method signature as below.

2. Use try..catch to handle exception in the method as given below.

Now you can see that the two approaches runs without compilation errors.

Unchecked exceptions

Unchecked exceptions are exceptions extended either from java.lang.RuntimeException or from java.lang.Error. They don’t need any handling to compile the code. But of course RuntimeException and its subclasses can be handled. A typical example of RuntimeException is ArithmeticException which is usually thrown when a number is divided by zero, and OutofMemoryError is an example of Error.

Creating UnChecked Exceptions

Ok, Lets try to create an UnChecked Exception. An example is given below.

Now we will follow the procedure we chose before. Create a test class for MyUnCheckedException. An example is given below.

Here we can see that the program compiles without any error even though the Exception is thrown.

Checked or Uncheked? Which should we choose

Even though checked and unchecked exception have no difference in their functionalities, there are a lot of controversies and arguments happening among developers on choosing the right one. But in the java documentation, there is a statement which may help us to choose one based on our situation and context; and that is.

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Hope this can help you to choose the right type of exception.

Leave a Comment

Your email address will not be published. Required fields are marked *