• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error-page

 
Yuqing Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm new here and have a question on error handling in servlet, hope you can help me.
My environment are winme, tomcat4.01, j2ee 1.3
see the following code fragement:
in servlet ...
//1 throw new UnavailableException ("unava", 2);
//2 throw new ServletException ();
//3 throw new RuntimeException();
throw new FileNotFoundException();
in web.xml ...
<error-page>
<exception-type>javax.servlet.UnavailableException</exception-type>
<location>/errorpage/unavailable.html</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorpage/servlet.html</location>
</error-page>
<error-page>
<exception-type>java.io.FileNotFoundException</exception-type>
<location>/errorpage/io.html</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/errorpage/runtime.html</location>
</error-page>
these codes compile. But only line 2 and 3 in servlet code work(the respective error page is displayed). For line 1, tomcat default error page is shown. For line 4, the only content shown are those output by servlet before exception thrown point.
From the previous posts in this forum, someone had the same problem with UnavailableException, but worked fine with java.net.MalformedURLException (subclass of IOException). Does anyone have any idea on this?
Thank for your help.
Regards,
Yuqing
[ January 29, 2002: Message edited by: Yuqing Zhu ]
 
Yuqing Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing should be mentioned for my pre post:
I changed line 4 to
throw new ServletException(new FileNotFoundException())
and comment out page-error for ServletException, the io.html is shown.
Thanks,
Yuqing
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand the API, the UnavailableException has a special meaning to the servlet container so it is probably caught elsewhere.
Bill
 
Yuqing Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William.
I agree UnavailableException is handled in some special way by container. But when I add a <error-code>503</error-code> in web.xml, all UnavailableException is redirect to my desired page. I believe that's <error-code> doing its job. So I think this maybe a work-around.
But how about IOException? As someone had success experience on it, so there must be something wrong in my codes, would anyone has some success codes pasted here, thanks.
Also funny enough, if I wrap UnavailableException in a ServletException, get rid of error-page for ServletException in web.xml, it works fine. So it seems only ServletException and RuntimeException work with <error-page> handling in my experience.
Regards,
Yuqing
[ January 29, 2002: Message edited by: Yuqing Zhu ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you wrapped those other exceptions in a ServletException?

I have never tried this, but you seem to think ServletExceptions and its subclasses are the ones that are appropriately handled by the container (which is probably correct).

[ January 29, 2002: Message edited by: Mike Curwen ]
 
Yuqing Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike. It did work if I wrap IOException in ServletException.
But as service() method declares throwing IOException, the container should handle it directly with error-page mechanism instead of leaving it out, shouldn't it!
Regards
Yuqing
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The service method throws IO and Servlet Exceptions, but who says the container has to handle those the same? In fact, I think you've proven in an indirect way, that they don't.

It seems that only ServletException or a subclass of this exception are handled by the 'error page' mechanism of the container.

Kinda sucky.

And perhaps I'm wrong, and there is something else going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic