I use a try/catch around the application launch and I install event handlers for all other uncaught exceptions. I've posted the main class for one of my applications below to show what I mean.
Larry.
Code:
package com.fhm.pdbm;
import java.io.File;
import java.lang.Thread.UncaughtExceptionHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.richclient.application.ApplicationLauncher;
import com.fhm.pdbm.error.ErrorUtils;
/**
* Main driver that starts the Physician Database Manager (PDBM) application
*
* @author lstreepy
*
*/
public class PDBManager {
private static Log _logger;
/**
* MAIN
*
* @param args
*/
public static void main(String[] args) {
try {
// Before we do anything, try to create the product root directory in
// the users home directory
createProductDir();
// Now we can create out logger
_logger = LogFactory.getLog( PDBManager.class );
_logger.info( "PDBManager starting up" );
// Install a general unhandled exception handler
Thread.setDefaultUncaughtExceptionHandler( new UEHandler() );
AWTExceptionHandler.register();
String rootContext = "/com/fhm/pdbm/ctx";
String startupContext = rootContext + "/startup-context.xml";
String applicationContext = rootContext + "/application-context.xml";
String businessLayerContext = rootContext + "/business-layer-context.xml";
String securityContext = rootContext + "/security-context.xml";
new ApplicationLauncher( startupContext, new String[] { applicationContext, businessLayerContext, securityContext } );
} catch( Exception e ) {
ErrorUtils.reportException( "Application Startup", e );
System.exit( 1 );
}
}
/**
* Create the product root directory.
*/
private static void createProductDir() {
File dir = new File( System.getProperty( "user.home" ), ".pdbm" );
if( !dir.exists() ) {
dir.mkdirs();
}
}
/**
* Class to handle uncaught exceptions.
*/
private static class UEHandler implements UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
String op = "Action on thread: " + t.getName();
ErrorUtils.reportException( op, e );
}
}
/**
* Another class to handle uncaught exceptions, this one for the AWT event thread.
* Since the AWT event thread catches exceptions (and just spits out a stack trace),
* they won't be "uncaught" and the handler above won't get invoked.
*/
public static class AWTExceptionHandler {
public static void register() {
System.setProperty( "sun.awt.exception.handler", AWTExceptionHandler.class.getName() );
}
public void handle(Throwable e) {
ErrorUtils.reportException( "Exception on AWT Event Thread", e );
}
}
}