Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab uncaught exceptions in a Java servlet web application

Is there a standard way to catch uncaught exceptions that happen inside of a java servlet container like tomcat or Jetty? We run a lot of servlets that come from libraries so we cannot easily put our on try/catch code. It would also be nice to in as generic of a way as possible catch and log all uncaught exceptions in our web application (which runs in Jetty) to our bug tracker via the API provided.

Please not I need to log the exceptions only, whether a a redirect is issues to a custom error page will not help me. We do everything via GWT-RPC so the user would never see an error page.

like image 859
benstpierre Avatar asked Sep 14 '11 01:09

benstpierre


People also ask

How do you handle exceptions in Java Web application?

There are two main mechanisms of exception handling in Java Web Application: Programmatically exception handling mechanism: When using try and catch block in the Java code (Servlet or Filter class) to handle exceptions. Declarative exception handling mechanism: When using the XML tags in the web.

How do you handle an exception in a servlet?

When a servlet throws an exception, the web container looks for a match with the thrown exception type in web. xml configurations that employ the exception-type element. To define the invocation of servlets in response to particular errors or HTTP status codes, you'd have to utilize the error-page element in web.

How do you handle uncaught exception in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.


2 Answers

I think a custom filter actually works best.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        chain.doFilter(request, response);
    } catch (Throwable e) {
        doCustomErrorLogging(e);
        if (e instanceof IOException) {
            throw (IOException) e;
        } else if (e instanceof ServletException) {
            throw (ServletException) e;
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            //This should never be hit
            throw new RuntimeException("Unexpected Exception", e);
        }
    }
}
like image 180
benstpierre Avatar answered Oct 02 '22 09:10

benstpierre


In web.xml (the deployment descriptor) you can use the <error-page> element to specify error pages by exception type or HTTP response status code. For example:

<error-page>
    <error-code>404</error-code>
    <location>/error/404.html</location>
</error-page>
<error-page>
    <exception-type>com.example.PebkacException</exception-type>
    <location>/error/UserError.html</location>
</error-page>

For a NetBeans-centric description, mosey on over to Configuring Web Applications: Mapping Errors to Error Screens (The Java EE 6 Tutorial) (or see the Java EE 5 Tutorial's version).

like image 31
Matt Ball Avatar answered Oct 02 '22 09:10

Matt Ball