Home  >  Article  >  Java  >  In Java, what does it mean to rethrow an exception?

In Java, what does it mean to rethrow an exception?

WBOY
WBOYforward
2023-09-01 16:57:051355browse

In Java, what does it mean to rethrow an exception?

When an exception is cached in a catch block, you can rethrow the exception using the throw keyword (used to throw an exception object).

When rethrowing an exception, you can throw the same exception as the unadjusted case -

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

Alternatively, wrap it in a new exception and throw it. When you wrap a cached exception in another exception and throw it, this is called exception chaining or exception wrapping, by doing this you can adapt your exception to throw a higher level exception, keeping 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();
}

Example

In the following Java example, our code may throw two exceptions, ArrayIndexOutOfBoundsException and ArithmeticException, in demoMethod(). We catch these two exceptions in two different catch blocks.

In the catch block, we rethrow the other exception directly by wrapping one of the exceptions in a higher-level exception.

The Chinese translation of

import java.util.Arrays;
import java.util.Scanner;
public class RethrowExample {
   public void demoMethod() {
      Scanner sc = new Scanner(System.in);
      int[] arr = {10, 20, 30, 2, 0, 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) {
         throw new IndexOutOfBoundsException();
      }
      catch(ArithmeticException e) {
         throw e;
      }
   }
   public static void main(String [] args) {
      new RethrowExample().demoMethod();
   }
}

Output1

is:

Output1

Array: [10, 20, 30, 2, 0, 8]
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)
0
4
Exception in thread "main" java.lang.ArithmeticException: / by zero
   at myPackage.RethrowExample.demoMethod(RethrowExample.java:16)
   at myPackage.RethrowExample.main(RethrowExample.java:25)

Output2

Array: [10, 20, 30, 2, 0, 8]
Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)
124
5
Exception in thread "main" java.lang.IndexOutOfBoundsException
   at myPackage.RethrowExample.demoMethod(RethrowExample.java:17)
   at myPackage.RethrowExample.main(RethrowExample.java:23)

The above is the detailed content of In Java, what does it mean to rethrow an exception?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete