You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从嵌套EJBException堆栈中捕获EvaluationException并读取newEvaluationRatesDTO

如何从嵌套异常中捕获EvaluationException并读取newEvaluationRatesDTO的值

给定以下异常堆栈信息:

javax.ejb.EJBException: java.lang.reflect.InvocationTargetException; nested exception is:
        com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException; nested exception is: com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException
com.emirates.jfoundation.exception.ServiceException: java.lang.reflect.InvocationTargetException
        at weblogic.utils.StackTraceDisabled.unknownMethod()
Caused by: java.lang.reflect.InvocationTargetException
        ... 1 more
Caused by: EvaluationException [oldEvaluationRatesDTO=null, **newEvaluationRatesDTO** =ShipmentEvaluationRatesDTO [id=null,

需要读取newEvaluationRatesDTO中的值,请问如何从上述堆栈对应的异常中捕获EvaluationException?

解决方案

方式一:逐层解析嵌套异常

根据堆栈的层级关系,异常嵌套顺序为:EJBException → ServiceException → InvocationTargetException → EvaluationException,可以逐层获取异常的cause:

try {
    // 执行触发异常的业务逻辑代码
} catch (EJBException ejbEx) {
    // 解析第一层异常:EJBException的cause是ServiceException
    Throwable serviceThrowable = ejbEx.getCause();
    if (serviceThrowable instanceof com.emirates.jfoundation.exception.ServiceException) {
        // 解析第二层异常:ServiceException的cause是InvocationTargetException
        Throwable invocationThrowable = serviceThrowable.getCause();
        if (invocationThrowable instanceof InvocationTargetException) {
            // 解析第三层异常:InvocationTargetException的cause是目标EvaluationException
            Throwable rootThrowable = invocationThrowable.getCause();
            if (rootThrowable instanceof EvaluationException) {
                EvaluationException evalEx = (EvaluationException) rootThrowable;
                // 获取newEvaluationRatesDTO对象
                ShipmentEvaluationRatesDTO ratesDto = evalEx.getNewEvaluationRatesDTO();
                // 读取DTO中的属性值,示例读取id
                if (ratesDto != null) {
                    Object shipmentId = ratesDto.getId();
                    // 后续业务处理逻辑
                }
            }
        }
    }
}

方式二:通用遍历嵌套异常(推荐)

如果异常嵌套层级可能发生变化,用循环遍历所有嵌套异常的方式更通用,无需硬编码层级:

try {
    // 执行触发异常的业务逻辑代码
} catch (EJBException ejbEx) {
    Throwable currentException = ejbEx;
    // 循环遍历所有嵌套异常,直到找到EvaluationException或无更多嵌套
    while (currentException != null) {
        if (currentException instanceof EvaluationException) {
            EvaluationException evalEx = (EvaluationException) currentException;
            // 获取目标DTO对象
            ShipmentEvaluationRatesDTO ratesDto = evalEx.getNewEvaluationRatesDTO();
            // 读取DTO属性
            if (ratesDto != null) {
                // 示例:输出id
                System.out.println("Shipment ID: " + ratesDto.getId());
                // 读取其他属性同理
            }
            break;
        }
        currentException = currentException.getCause();
    }
}

注意事项

  • 确保EvaluationException类提供了获取newEvaluationRatesDTO的getter方法(如getNewEvaluationRatesDTO()),如果没有公开的getter,可能需要通过反射读取私有字段。
  • 处理过程中要注意空指针判断,避免因某个异常的cause为null导致报错。

内容的提问来源于stack exchange,提问作者themaster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 17:50:36