Wednesday, April 21, 2004

Catching Java Errors in the main() Method.

I've been using Java for more years than I care to mention but I never realized that you could catch errors as well as exceptions. So I've just added one more item to my toolbox. My main() method always uses this template:

try {
  // do something.
} catch (Error e) {
  e.printStackTrace();
} catch (Exception e) {
  e.printStrackTrace();
} finally {
  System.out.println("Done.");
}

Of course, in my real code I use log4j so that the errors are persisted.

Post a Comment