You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cm...@yahoo.com on 2001/02/05 21:38:02 UTC

RequestDispatcher and exceptions

Hi,

I'm working on few bugs related with RequestDispatcher ( well, it started
with parameter handling, but RequestDispatcher is the place that have to
be resolved first in order to resolve parameters - since it is doing
special things with the params ).

The main issue is what should happen if an exception is thrown in
include() ?

The current behavior is that the error handler is called, and it displays
the "500 whatever" or the error page for that exception.

But by reading the spec, it seems that the "correct" thing to do is to 
just throw the exception - 
include() does have a throw declaration and is documented to throw
exceptions. 

My understanding is that "error handlers" ( as declared in server.xml or
the implicit ones ) should be called only on the top-level request.
( a servlet including another one may be able to deal with the errors - if
it doesn't catch them they will be thrown anyway )..

The particular test is /test/servlet/dispatch.PrintWriterTest1Servlet
where it expects the error in include to be reported instead of thrown.


My preference would be to fix the test and throw exceptions from
include(). On the other side this changes the behavior of tomcat - and I
need a second opinion. If indeed the include() is supposed to throw the
exception, then it's a spec issue and the previous behavior doesn't
matter.


-- 
Costin


Re: RequestDispatcher and exceptions

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
cmanolache@yahoo.com wrote:

> > > > In Tomcat 4, the following rules are applied:
> > > > * If the included servlet throws ServletException or IOException,
> > > >   propogate it on to the calling servlet (i.e. no error page behavior).
> > > > * If the included servlet throws any other exception (such as
> > > >   NullPointerException), wrap it in a ServletException and propogate
> > > >   that to the calling servlet (i.e. no error page behavior).
> > >
> > > Great, I'll do something similar in tomcat3.3.
> > >
> > > My only issue is with the special treatement of RuntimeExceptions - is
> > > there a need to wrap them with ServletException ? I would treat
> > > included servlets in the same way as "normal" servlets ( from error
> > > handling perspective).
> > >
> >
> > Just the spec requirement (Section 8.5 for 2.3 PFD; I'm sure the 2.2 spec had a
> > similar statement.
>
> ???
>
> Section 8.5 in 2.3 PFD says:
> "Only runtime exceptions and checked exceptions of type ServletException
> and IOException should be propagated ... if thrown by the target of
> RequestDispatcher. All other exceptions should be wrapped ... "
>
> My understanding of this is that NullPointerException should be
> propagated, not wrapped, same for security and any other RuntimeException.
>

Mea culpa.  Costin's interpretation is correct (I was too focused on the "checked
exceptions" part of the statement quoted above), and Tomcat 4 is currently broken in
this respect.  That will get changed tonight.


>
> Costin
>

Craig



Re: RequestDispatcher and exceptions

Posted by cm...@yahoo.com.
> > > In Tomcat 4, the following rules are applied:
> > > * If the included servlet throws ServletException or IOException,
> > >   propogate it on to the calling servlet (i.e. no error page behavior).
> > > * If the included servlet throws any other exception (such as
> > >   NullPointerException), wrap it in a ServletException and propogate
> > >   that to the calling servlet (i.e. no error page behavior).
> >
> > Great, I'll do something similar in tomcat3.3.
> >
> > My only issue is with the special treatement of RuntimeExceptions - is
> > there a need to wrap them with ServletException ? I would treat
> > included servlets in the same way as "normal" servlets ( from error
> > handling perspective).
> >
> 
> Just the spec requirement (Section 8.5 for 2.3 PFD; I'm sure the 2.2 spec had a
> similar statement.

???

Section 8.5 in 2.3 PFD says:
"Only runtime exceptions and checked exceptions of type ServletException
and IOException should be propagated ... if thrown by the target of
RequestDispatcher. All other exceptions should be wrapped ... "

My understanding of this is that NullPointerException should be
propagated, not wrapped, same for security and any other RuntimeException.

Costin






Re: RequestDispatcher and exceptions

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
cmanolache@yahoo.com wrote:

> > > My preference would be to fix the test and throw exceptions from
> > > include(). On the other side this changes the behavior of tomcat - and I
> > > need a second opinion. If indeed the include() is supposed to throw the
> > > exception, then it's a spec issue and the previous behavior doesn't
> > > matter.
> > >
> >
> > In Tomcat 4, the following rules are applied:
> > * If the included servlet throws ServletException or IOException,
> >   propogate it on to the calling servlet (i.e. no error page behavior).
> > * If the included servlet throws any other exception (such as
> >   NullPointerException), wrap it in a ServletException and propogate
> >   that to the calling servlet (i.e. no error page behavior).
>
> Great, I'll do something similar in tomcat3.3.
>
> My only issue is with the special treatement of RuntimeExceptions - is
> there a need to wrap them with ServletException ? I would treat
> included servlets in the same way as "normal" servlets ( from error
> handling perspective).
>

Just the spec requirement (Section 8.5 for 2.3 PFD; I'm sure the 2.2 spec had a
similar statement.

>
> ( I'm thinking of Security exceptions - all of them are Runtime, and the
> "common" behavior is to catch them if you expect them. )
>
> In any case, this is a minor detail ( compared with the decision to throw
> or not).
>
> Costin
>

Craig



Re: RequestDispatcher and exceptions

Posted by cm...@yahoo.com.
> > My preference would be to fix the test and throw exceptions from
> > include(). On the other side this changes the behavior of tomcat - and I
> > need a second opinion. If indeed the include() is supposed to throw the
> > exception, then it's a spec issue and the previous behavior doesn't
> > matter.
> >
> 
> In Tomcat 4, the following rules are applied:
> * If the included servlet throws ServletException or IOException,
>   propogate it on to the calling servlet (i.e. no error page behavior).
> * If the included servlet throws any other exception (such as
>   NullPointerException), wrap it in a ServletException and propogate
>   that to the calling servlet (i.e. no error page behavior).

Great, I'll do something similar in tomcat3.3. 

My only issue is with the special treatement of RuntimeExceptions - is
there a need to wrap them with ServletException ? I would treat
included servlets in the same way as "normal" servlets ( from error
handling perspective). 

( I'm thinking of Security exceptions - all of them are Runtime, and the
"common" behavior is to catch them if you expect them. )

In any case, this is a minor detail ( compared with the decision to throw
or not).

Costin 




Re: RequestDispatcher and exceptions

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
cmanolache@yahoo.com wrote:

> Hi,
>
> I'm working on few bugs related with RequestDispatcher ( well, it started
> with parameter handling, but RequestDispatcher is the place that have to
> be resolved first in order to resolve parameters - since it is doing
> special things with the params ).
>
> The main issue is what should happen if an exception is thrown in
> include() ?
>
> The current behavior is that the error handler is called, and it displays
> the "500 whatever" or the error page for that exception.
>
> But by reading the spec, it seems that the "correct" thing to do is to
> just throw the exception -
> include() does have a throw declaration and is documented to throw
> exceptions.
>
> My understanding is that "error handlers" ( as declared in server.xml or
> the implicit ones ) should be called only on the top-level request.
> ( a servlet including another one may be able to deal with the errors - if
> it doesn't catch them they will be thrown anyway )..
>
> The particular test is /test/servlet/dispatch.PrintWriterTest1Servlet
> where it expects the error in include to be reported instead of thrown.
>
> My preference would be to fix the test and throw exceptions from
> include(). On the other side this changes the behavior of tomcat - and I
> need a second opinion. If indeed the include() is supposed to throw the
> exception, then it's a spec issue and the previous behavior doesn't
> matter.
>

In Tomcat 4, the following rules are applied:
* If the included servlet throws ServletException or IOException,
  propogate it on to the calling servlet (i.e. no error page behavior).
* If the included servlet throws any other exception (such as
  NullPointerException), wrap it in a ServletException and propogate
  that to the calling servlet (i.e. no error page behavior).

In other words, I understood the spec the same way you did.  Error pages only
happen at the top-level servlet.

>
> --
> Costin
>

Craig