If you are using JDK 5 you can set the
thread's
uncaught exception handler using:
Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) If you are in in JDK 4 you can create a custom
ThreadGroup which overrides the
uncaughtException(Thread t, Throwable e) and make your threads part of that group.
However, in jDK 4, that technique would exclude threads created directly by the JVM like the main thread and the AWT-Event Dispatch Thread.
To catch those exceptions caused in the AWT-Event Dispatch thread, in JKD 4, you must set a
system property :
System.setProperty("sun.awt.exception.handler", "ErrorHandler");
Where
ErrorHandler is a class with the following structure:
Even another alternative is to attach a shutdown hook to your runtime. I ran the code below with very low memory settings to make it fail on purpose because of OutOfMemoryError. Look how the showdown hook thread is executed just before exiting the application.
>
java -Xms1M -Xmx2M Question1
Be sure to create the shutdown hook thread before your processing, or there could be not enough memory to create the thread.
A final alternative is to manipulate the amount of memory available with the -Xms and -Xmx flags provided by the Java Hotspot Virtual Machines. This way you may avoid the error at all.
I hope this helps!
[ June 01, 2006: Message edited by: Edwin Dalorzo ]