Java的catch块中throw e和throw new Exception(e)有什么区别?

Exception(异常)是程序执行期间发生的问题(运行时错误)。这是一些示例方案-

  • 如果您的数组大小为10,如果代码中的一行尝试访问该数组中的第11个元素。

  • 如果您试图用0除以一个数字(结果是无穷大,而JVM不知道如何计算它的值)。

发生异常时,程序会在导致异常的行突然终止,从而使程序的其余部分无法执行。为防止这种情况,您需要处理异常。

java中有两种类型的异常。

  • 未检查的异常 - 未检查的异常是在执行时发生的异常。这些也称为运行时异常。其中包括编程错误,例如逻辑错误或API使用不当。编译时将忽略运行时异常。

  • 检查异常 - 检查异常是在编译时发生的异常,也称为编译时异常。这些异常不能在编译时简单地忽略。程序员应注意(处理)这些异常。

处理异常

为了处理异常,Java提供了try-catch块机制。

在可能产生异常的代码周围放置了一个try / catch块。try / catch块中的代码称为受保护代码。

语法

try {
   // Protected code
} catch (ExceptionName e1) {
   // Catch block
}

当异常在try块内引发时,JVM将异常详细信息存储在异常堆栈中,并继续处理catch块,而不是终止程序。

catch语句涉及声明您要捕获的异常类型。如果try块中发生异常,将验证try之后的catch块。

如果在catch块中列出了发生的异常类型,则将异常传递给catch块的方式与将参数传递给方法参数的方式一样。

示例

import java.io.File;
import java.io.FileInputStream;
public class Test {
   public static void main(String args[]){
      System.out.println("Hello");
      try{
         File file =new File("my_file");
         FileInputStream fis = new FileInputStream(file);
      }catch(Exception e){
         System.out.println("找不到给定的文件路径");
      }
   }
}

输出结果

找不到给定的文件路径

重新抛出异常

当将异常缓存在catch块中时,可以使用throw关键字(用于抛出异常对象)将其重新抛出。

重新抛出异常时,您可以抛出相同的异常,而无需将其调整为-

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

或者,将其包装在新异常中并抛出。 当您将缓存的异常包装在另一个异常中并抛出它时,这称为异常链接或异常包装,通过这样做,您可以调整您的异常,抛出维护抽象的更高级别的异常。

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

示例

在以下Java示例中,我们在demoMethod()中的代码可能会抛出ArrayIndexOutOfBoundsException和ArithmeticException。我们在两个不同的catch块中捕获了这两个异常。

在catch块中,我们通过包装在更高的异常中来抛出两个异常,而另一个则直接抛出。

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

输出结果

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)

输出结果

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)