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 ]
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 ]
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 ]
Joined: Jan 17, 2002
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