Tuesday, January 10, 2012

Catching Uncaught Exceptions

Having stack traces of uncaught exceptions show up in System.err is not ideal. These
messages are confusing to end users if they happen to see them, and they are not
available for diagnostic purposes when you need them. A better approach is to log
them to a file. As of Java SE 5.0, you can change the handler for uncaught exceptions
with the static Thread.setDefaultUncaughtExceptionHandler method:
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
save information in log file
};
});

No comments:

Post a Comment