简体   繁体   中英

Why doesn't EJBException use Throwable.cause?

Quite for a long time I'm wondering why doesn't EJBException use standard Throwable.cause field to reach an exception it wraps?

It complicates getting the original root cause to something like that

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: I'm on Java6 and 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 method was retrofitted), but it sounds like your application server does not do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM