14

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.

benstpierre
  • 32,833
  • 51
  • 177
  • 288

3 Answers3

14

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);
        }
    }
}
benstpierre
  • 32,833
  • 51
  • 177
  • 288
  • 2
    The post about using `error-page` is the right way to do this in a servlet environment. – nilskp Apr 25 '13 at 16:24
  • 1
    Actually the above javax.servlet.Filter solution works mighty fine in a servlet environment. And I like the fact that we don't have to edit the web.xml for it. – David Apr 03 '14 at 10:53
  • 1
    this example would cause the side effect of catching Errors (a non-Exception subclass of throwable and converting them to Exceptions). Since Errors are internal issues relating to the JVM itself (like OutOfMemoryError, or ThreadDeath). Since the JVM itself needs to handle errors, its a bad practice to catch throwables and convert them to an Exception that won't be handled by the JVM appropriately. ( http://stackoverflow.com/questions/6083248/is-it-a-bad-practice-to-catch-throwable) – Andrew Norman Jan 27 '16 at 22:25
11

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).

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 4
    It's worth pointing out that the error handling servlet code can retrieve the exception from the `request` object in the `javax.servlet.error.exception` attribute. E.g.: `request.getAttribute("javax.servlet.error.exception")` – nilskp Apr 25 '13 at 16:26
3

Not 100% sure if this will work with a servlet container, or how far upstream this call would need to go, but you can call the static setDefaultUncaughtExceptionHandler method on Thread to set a handler that will handle all uncaught exceptions.

Matt Wonlaw
  • 12,146
  • 5
  • 42
  • 44