Java Global Exception Handling
It’s been a while since I have worked with Java and it’s time for me to brush up so I have decided to do 30 days of Java. I will be attempting to learn something new or explain something about Java each day.
In many of my projects I use a remote error collecting service called ErrorStack. Love it! My apps throw errors over the internet to it and it generates report and has a myriad of ways to alert me. I do my best to write code that doesn’t have errors and handles errors and exception gracefully but it still happens. This is where the Java 1.5 setDefaultUncaughtExceptionHandler comes in to play.
I wrote some quick code to test out the feature
package org.michaelgrace.sandbox;
public class GlobalErrorHandling {
public static void main(String[] args) {
Handler handler = new Handler();
Thread.setDefaultUncaughtExceptionHandler(handler);
throw new RuntimeException("party on error!");
}
}
class Handler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Where did that error come from?!");
System.out.println("This is where I would report the error to ErrorStack");
System.out.println(e.getMessage());
System.out.println(t.toString());
}
}
I exported the class to a jar using Eclipse and ran it from the command line
mgrace$ java -jar global-error-test.jar
Where did that error come from?!
This is where I would report the error to ErrorStack
party on error!
Thread[main,5,main]
It worked!
I plan on building a class for making it easy to report errors to ErrorStack and will release it to the world when I do.
Resources: http://www.nomachetejuggling.com/2006/06/13/java-5-global-exception-handling/