Java异常处理:try、throw与getCause的综合应用
异常结构分析
在Java中,异常的层次结构可以通过连续调用getCause()方法来追溯。例如,如果异常A是由异常B引发的,而异常B又是由异常C引发的,那么可以通过以下方式获取到最底层的异常C:
Throwable rootCause = e.getCause().getCause();
其中:
- e 表示当前异常A;
- e.getCause() 表示异常B;
- e.getCause().getCause() 表示异常C。
案例演示1:异常的相互抛出
1. 自定义异常类
首先定义两个自定义异常类CapitalException和CityException:
public class CapitalException extends RuntimeException {
public CapitalException(String message) {
super(message);
}
public CapitalException(String message, Throwable cause) {
super(message, cause);
}
}
public class CityException extends RuntimeException {
public CityException(String message) {
super(message);
}
public CityException(String message, Throwable cause) {
super(message, cause);
}
}
2. 模拟调用场景
接下来模拟一个场景,展示异常如何在不同层级之间传递:
public class ExceptionDemo {
public static void main(String[] args) {
try {
methodA();
} catch (Throwable e) {
System.out.println("捕获的异常:" + e);
System.out.println("异常原因:" + e.getCause());
System.out.println("根本原因:" + e.getCause().getCause());
}
}
private static void methodA() {
try {
methodB();
} catch (CityException e) {
throw new CapitalException("首都异常", e);
}
}
private static void methodB() {
try {
methodC();
} catch (NullPointerException e) {
throw new CityException("城市异常", e);
}
}
private static void methodC() {
throw new NullPointerException("空指针异常");
}
}
案例演示2:异常封装与重新抛出
在这个场景中,我们将异常从低层逐步向上抛出,并在每一层进行封装:
public class ExceptionEncapsulationDemo {
public static void main(String[] args) {
try {
processA();
} catch (Throwable e) {
System.out.println("最终捕获的异常:" + e);
System.out.println("异常原因:" + e.getCause());
}
}
private static void processA() {
try {
processB();
} catch (CityException e) {
throw new CapitalException("封装后的异常", e);
}
}
private static void processB() {
try {
processC();
} catch (NullPointerException e) {
throw new CityException("内部异常封装", e);
}
}
private static void processC() {
throw new NullPointerException("原始异常");
}
}
关于throw与throws的区别
在Java中,throw用于在方法体内显式抛出异常,而throws则用于声明方法可能抛出的异常类型。
- 如果自定义异常继承自
RuntimeException,则无需显式使用throws关键字。 - 如果继承自
Exception,则必须使用throws关键字进行声明。
例如:
public void mayThrowUnchecked() {
throw new RuntimeException("非受检异常");
}
public void mayThrowChecked() throws IOException {
throw new IOException("受检异常");
}
throw与try-catch的关系
在try块中抛出异常时,catch块可以根据异常类型进行捕获。以下是两种常见情况:
1. 捕获Throwable类型的异常
try {
throw new Exception("测试异常");
} catch (Throwable e) {
System.out.println("捕获到异常:" + e.getMessage());
}
2. 捕获Exception类型的异常
try {
throw new IllegalArgumentException("非法参数异常");
} catch (Exception e) {
System.out.println("捕获到异常:" + e.getMessage());
}