I’ve recently spent some hours to track down a mysterious bug and I just couldn’t find it. Since my application is using a lot of Threads/Workers it came to my mind that a RuntimeException I did not catch could be the cause. I wrote a global handler for it and found where the problem was :)

Somewhere in main():

Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler ());

The class:

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.err.println (“Uncaught exception by “ + t + “:”);

How to test it:

new Thread () { public void run () { throw new RuntimeException (“x”);}}.start ();

PermGen space exceptions with Maven due to memory leak
Drawing in Java
Selecting for write in a Java Selector

Are you interested in reading more from CodingClues?
Then subscribe to new postings via RSS or via E-Mail.

close Reblog this comment
blog comments powered by Disqus