为什么 EJBException 不使用 Throwable.cause?

Why doesn't EJBException use Throwable.cause?

很长一段时间我想知道为什么 EJBException 使用标准 Throwable.cause 字段来达到一个异常 wraps?

它使找到像这样的事情的原始根本原因变得复杂

private String getRootCauseErrorMessage(final Exception ex) {
    Throwable currentException = ex;
    Throwable nextException = null;
    do {
        if (nextException != null) {
            currentException = nextException;
        }

        /* For some reason EJBException stores cause in a separate field rather the all generic Throwables */
        if (currentException instanceof EJBException) {
            nextException = ((EJBException) currentException).getCausedByException();
        } else {
            nextException = currentException.getCause();
        }
    } while (nextException != null);

    return currentException.getMessage();
}

ps:我在 Java6 和 EJB3

Throwable.getCause was not added until Java 1.4. Some implementations of EJBException do retrofit the getCausedByException method to use the getCause method (similar to how the RemoteException.getCause 方法进行了改造),但听起来您的应用程序服务器没有这样做。