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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.vidvaan.corejava.exception16.handleUnchecked; // unchecked exception - not checked by compiler public class Example01UncheckedException { public static void main(String[] args) { System.out.println("main begin"); int num1 = 1;// may get data from user int num2 = 0;// may get data from user // Since I'm dividing an integer with 0 it should throw unchecked // ArithmeticException // But Compiler is not demanding us to handle the exception here int res = num1 / num2; System.out.println(res); System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.vidvaan.corejava.exception16.handleUnchecked; // unchecked exception - handling with try-catch block - wrong option public class Example02UncheckedExceptionHandle { public static void main(String[] args) { System.out.println("main begin"); int num1 = 1;// may get data from user int num2 = 0;// may get data from user // Here we are handling uncheckd exceptions with try-catch try { // Since I'm dividing an integer with 0 it should throw ArithmeticException int res = num1 / num2; System.out.println(res); } catch (ArithmeticException e) { System.out.println("Can't Divide with Zero !!! " + e.getMessage()); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.vidvaan.corejava.exception16.handleUnchecked; // unchecked exception - handling with pre-conditions - correct option public class Example03UncheckedExceptionPreConditions { public static void main(String[] args) { System.out.println("main begin"); int num1 = 1;// may get data from user int num2 = 0;// may get data from user if (num2 != 0) { int res = num1 / num2; System.out.println(res); } else { System.out.println("Can't Divide with Zero !!! "); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.exception16.handleUnchecked; // unchecked exception - declared by library method public class Example04LibraryMehtodDeclareUnchecked { public static void main(String[] args) { System.out.println("main begin"); // Here we don't have option to write pre-condition to avoid abnormal // termination libraryMethod(); System.out.println("main end"); } // Here library method declaring unchecked exception IllegalArgumentException public static void libraryMethod() throws IllegalArgumentException { if (true) { throw new IllegalArgumentException("Something went wrong !!!"); } } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.vidvaan.corejava.exception16.handleUnchecked; // unchecked exception - declared by library method - fix with try catch block public class Example05HandleLibraryMehtodDeclaredUnchecked { public static void main(String[] args) { System.out.println("main begin"); // Here we are handling unchecked exception with try-catch block try { // Here we don't have option to write pre-condition to avoid abnormal // termination libraryMethod(); } catch (IllegalArgumentException e) { System.out.println("Error in libraryMethod !!! : " + e.getMessage()); } System.out.println("main end"); } // Here library method declaring unchecked exception IllegalArgumentException public static void libraryMethod() throws IllegalArgumentException { if (true) { throw new IllegalArgumentException("Something went wrong !!!"); } } } |
In the above example, We are handling unchecked exception with try-catch block.