Solved

Servlet Container Exception Handling

Posted on 2011-09-06
3
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

632 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