ServletException seems to behave differently than expected when wrapping
other Throwables, and I'm wondering if there is a reason for this.  A bit
more explanation: in general, since JDK 1.4, any subclass of Throwable can
be used to wrap another Throwable passed as an arg to its constructor (see
the JDK 1.4 docs for Throwable).  The passed Throwable arg becomes the
"cause" of the new Exception.  This can be extended, so that the whole chain
of causes can be recorded.  This can be useful for logging/debugging, and
the whole chain of causes can be logged with their stacktraces if required.

However, the problem is that when trying to chain by wrapping with
ServletException, it seems to not wrap the underlying cause correctly, in
that it cannot be retrieved using Throwable's getCause() method later.
Instead, the cause has to be retrieved using ServletException's own
getRootCause() method.  So in the case below, the output is "null" then
"java.lang.Exception: My error message":

import javax.servlet.ServletException;

public class ServletExceptionTest
{
        public static void main(String[] args)
        {
                Exception e = new Exception("My error message");
                ServletException se = new ServletException(e);
                System.out.println(se.getCause());
                System.out.println(se.getRootCause());
        }
}

This means having to write specific code to handle ServletExceptions, rather
than being able to treat all Throwables generically.  This seems to go
against the benefits of inheritance...?  Perhaps ServletException was not
retrofitted to the "cause" approach when it was introduced in JDK 1.4?? Or
is there something more to it than that which I'm missing?



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to