Solved

Servlet Container Exception Handling

Posted on 2011-09-06
3
195 Views
Last Modified: 2012-05-12
Hello,

I have a J2EE application running on Tomcat 7 with log4j implemented.  I wanted to create a custom error page whenever my application encounters an Exception.  I created something similar to ex #1 below in my web.xml, but I was wondering If I can just catch all errors.  I don't think its possible since 500 is java.lang.Throwable and wont get caught if I try to use and exception-type condition (see #2 below).  

ex#1
<error-page>
    <error-code>404</error-code>
    <location>/errors/error.jsp</location>
<error-code>500</error-code>
    <location>/errors/error.jsp</location>
</error-page> 

Open in new window


ex#2
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errors/error.jsp</location>  
</error-page> 

Open in new window


0
Comment
Question by:cgray1223
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36492523


perhpas this solution can help you:
http://www.coderanch.com/t/492765/Servlets/java/Exception-Handling-Servlets


      
posted Saturday, April 24, 2010 2:02:26 AM private message
Quote
If you wrap your MyException in a ServletException declare the ServletException in your web.xml file

Example:



    catch(MyException e)    
    {    
        throw new ServletException(e)  
    }  



web.xml file:


      <error-page>  
       <exception-type>javax.servlet.ServletException</exception-type>  
       <location>/error/error.jsp</location>  
      </error-page>  

also read some stuff below in that link
0
 

Author Comment

by:cgray1223
ID: 36497172
Thanks for the response.  That would work for all exceptions I'm expecting, but not for 404s or runtime exceptions that are not trapped.  I don't think I can trap every error that could ever occur...thoughts?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36497380
Yes, I think you are right - you cannot trap every error that could ever occurs - there will be alwyas something which you had not anticipated
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question