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

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

WBOY
Release: 2023-09-01 16:57:05
forward
1400 people have browsed it

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;
}
Copy after login

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();
}
Copy after login

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();
   }
}
Copy after login

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)
Copy after login

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)
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template