How to handle unchecked exceptions

Some times unchecked exception occurs due to the bad data provided by user during the user-application interaction. It is the responsibility of the developer to have pre-conditions in advance to avoid operations(which intern leads to unchecked exceptions) on invalid data. So some of the unchecked exceptions are usually caused by the mistake of the programmer rather than the external recourses and environment. But we may not have option to write pre-conditions for certain unchecked exceptions, In those cases unchecked exceptions should be handled with try-catch block only. Let us discuss the with the following example.

Example 01: unchecked exception – no compilation errors

Output :

main begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception16.handleUnchecked.Example01UncheckedException.main(Example01UncheckedException.java:14)

In the above example, We are not getting any compilation error even there is a chance of getting ArithmenticException. It clear that unchecked exceptions are not checked by the compiler.

Example 02: unchecked exception – fix with try catch block – wrong option

In the above example, We are handling the exception with try-catch block. It runs fine and displays why it is unable to divide and more importantly its not abnormally terminate the program. But we never handle ArithmeticException with try-catch block. ArithmeticException could be handled with pre-conditions as shown in the next example.

Example 03: unchecked exception – fix with pre conditions – correct option

In the above example, We are handling the issue with pre-conditions. It runs fine and displays why it is unable to divide and no concept of exception, no handling code no abnormal termination. This is the best way of handling ArithmeticException.

We never handle unchecked exceptions like NullPointerException, ArithmeticException, NumberFormatException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, IllegalArgumentException, ClassCastException with try-catch block. We could avoid them with pre-conditions.

But we may not have option to write pre-conditions for certain unchecked exceptions, Then we should handle them with try-catch block only. Let’s see an example.

Example 04 – unchecked exception – declared by library method

In the above example, libraryMethod() method declares unchecked exception IllegalArgumentException. We are calling libraryMethod() method from main(). As it is unchecked exception no compilation error. But when we run this program it will terminate the program abnormally. Here we don’t have option to have pre-condition to avoid IllegalArgumentException. So we should use try-catch block to avoid abnormal termination of the program as shown in the below example.

Example 05: unchecked exception – declared by library method – fix with try catch block

In the above example, We are handling unchecked exception with try-catch block.

Scroll to Top