Solved

Servlet Container Exception Handling

Posted on 2011-09-06
3
188 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now