While chaining, can we throw unchecked exception from a checked exception in java?


When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).

While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −

try {
   int result = (arr[a])/(arr[b]);
   System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}catch(ArithmeticException e) {
   throw e;
}

Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher level of exception maintaining the abstraction.

try {
   int result = (arr[a])/(arr[b]);
   System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}catch(ArrayIndexOutOfBoundsException e) {
   throw new IndexOutOfBoundsException();
}

Throwing unchecked exception from a checked exception

Yes, we can catch compile time exception (checked) and in the catch block we can wrap it with in a run time exception (unchecked) and re-throw it. But since we are re-throwing using checked exception we need to either wrap it inside an implicit try-catch pair or, skip handling it using the throws clause.

Example

In the following Java example we have created a user defined (checked) exception named SampleException.

We are displaying an integer array of 6 elements and letting user to select the position of the two values and dividing the selected numbers. While choosing the position the user my use index value, beyond the length of the array, which causes an ArrayIndexOutOfBoundsException which is unchecked exception.

In the catch block we are re-throwing this object by wrapping it with in the above created Sample exception, which is checked.

import java.util.Arrays;
import java.util.Scanner;
class SampleException extends Exception {
   SampleException(String msg){
      super(msg);
   }
}
public class Rethrow {
   public void demoMethod() {
      Scanner sc = new Scanner(System.in);
      int[] arr = {10, 20, 30, 2, 5, 8};
      System.out.println("Array: "+Arrays.toString(arr));
      System.out.println("Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)");
      int a = sc.nextInt();
      int b = sc.nextInt();
      try {
         int result = (arr[a])/(arr[b]);
         System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
      }catch(ArrayIndexOutOfBoundsException e) {
         try {
            throw new SampleException("This is a checked exception");
         } catch (SampleException e1) {
            System.out.println("Checked exception in the catch block");
         }
      }
   }
   public static void main(String [] args) {
      new Rethrow().demoMethod();
   }
}

Output

Array: [10, 20, 30, 2, 0, 8]
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)
25
24
Checked exception in the catch block

Updated on: 03-Jul-2020

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements