在Java的catch块中,`throw e`和`throw new Exception(e)`有什么区别?
异常是在程序执行期间发生的运行时错误。以下是一些示例场景:
- 如果你有一个大小为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("Given file path is not found");
}
}
}输出
Given file path is not found
重新抛出异常
当异常在 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)
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP