自定义 RuntimeException 类不是自定义异常类的实例。但 RuntimeException 是 Exception 的实例。为什么???
我为我的项目创建了两个异常类:
public class ABCRuntimeException extends RuntimeException {
//Functions...
}
public class ABCException extends Exception {
//Functions...
}
现在我正在尝试像这样处理这些异常:
try{
//Code which throw ABCRuntimeException
} catch(Exception e) {
if(e instanceof ABCException) {
System.out.println("Is instance of ABCException");
} else if(e instanceof Exception) {
System.out.println("Is instance of Exception");
}
}
输出是Is instance of Exception
。为什么 ABCRuntimeException 不是 ABCException 的实例。
以及如何处理这种异常情况,以便我可以为 ABCRuntimeException 和 ABCException 应用通用逻辑,而无需将 OR 运算符置于 if 条件中?