Global Exception Handling in Java

It's considered a best practice to only catch an exception if you plan to do something with it. Oftentimes, developers feel the need to capture exceptions in their code when all they intend to do with them is log them. Rather than sprinkle try/catch blocks throughout your applications, try using global exception handlers.
Global exception handlers handle any uncaught exception in an application. This allows you to follow best practices by allowing exceptions to bubble up and gives you a last-minute chance to do something with the exceptions, like log them. Additionally, global exception handlers are a great way of programming defensively - they enable you to catch exceptions which you may have missed.
Here's how to set up global exception handling in Java.
Global Exception Handling with Pure Java
The Java programming language provides a built-in facility for defining a global exception handler. There are two steps involved in getting global exception handling rolling in your application.
The first step is to define your own UncaughtExceptionHandler implementation class. The interface UncaughtExceptionHandler is defined at: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html You must implement the interface UncaughtExceptionHandler with a custom class definition which implements the method uncaughtException(Thread t, Throwable e). Within the uncaughtException method, you can specify what to do with uncaught exceptions, such as log them, email them, or (not advised) ignore them. Here is a reference implementation:
import java.lang.Thread.UncaughtExceptionHandler;
public class CustomExceptionHandler implements UncaughtExceptionHandler
{
public void uncaughtException(Thread t, Throwable e)
{
System.out.println("I caught an exception: " + e.getMessage());
}
}
The second step is to set the default uncaught exception handler. You do this with the Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler) method call, defined here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29
This will tell the execution runtime that anytime an uncaught exception is encountered, to forward it to the defined UncaughtExceptionHandler implementation. A reference implementation for setting the default uncaught exception handler is as follows:
public class Main
{
public static void main(String[] args) throws Exception
{
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());
throw new Exception("I am exceptional!");
}
}
Global Exception Handling in Tomcat
Now, what happens when you don't have a clear entry point established for a thread? How do you establish where to set the uncaught exception handler? Usually this occurs when you are using a framework, and usually frameworks provide their own ways for setting global exception handlers. Tomcat provides such a mechanism through the error-page web.xml directive. In it, you map exception types to page handlers. To set up a catch-all, use Throwable as the type, as shown in the following reference implementation:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
Then, you need to set up your handler page as you defined it. Accessing the exception is a little bit different than it is in standard Java - in Tomcat, you need to access the individual attributes from the request object. Tomcat assigns these attributes when an exception is encountered and then hands over control to the error page you have defined. In essence, Tomcat has its own global exception handler in place and allows you to participate in its exception handling process via the error-page directive. Here is a reference implementation for an error page that retrieves the status code of the error:
<%
Throwable t = (Throwable)request.getAttribute("javax.servlet.error.exception");
%>
<h1><%= request.getAttribute("javax.servlet.error.status_code") %></h1>
The following attributes are set by Tomcat and are available within your error page:
| javax.servlet.error.status_code | java.lang.Integer |
| javax.servlet.error.exception_type | java.lang.Class |
| javax.servlet.error.message | java.lang.String | javax.servlet.error.exception | java.lang.Throwable |
| javax.servlet.error.request_uri | java.lang.String |
| javax.servlet.error.servlet_name | java.lang.String |

Pingback: Elektrische Zahnbuerste