Runnable’s and logging Exceptions

When creating Runnable’s (or other task related structures like Callable’s or your own Threads), make sure that you are logging exceptions. This week I was asked by a colleague to assist with a possible concurrency problem. The symptom was the disappearance of running tasks.

We added some extra debugging statements and added the name of the thread to the logging, but we still were not able to find the cause. After 15 minutes we figured out that the cause could be an exception that was not logged and therefore not visible in the logging. So we added a try/catch block around the content of the runnable, ran the application, and what do you guess? In the log stacktraces started to appear.

So when you are in charge of the top of the callstack (so running your own threads) make sure that you catch and log exceptions. If you don’t, it could lead to hard to find problems.

Advertisements

3 Responses to Runnable’s and logging Exceptions

  1. Kieron says:

    I find a default uncaught exception handler does the trick!

    Thread.setDefaultUncaughtExceptionHandler(…

  2. Georgi says:

    Yeah, such exceptions do hurt. I try to use the Java ThreadGroup to control this: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ThreadGroup.html#uncaughtException(java.lang.Thread,%20java.lang.Throwable)
    Has the advantage that every uncaught exception of every Thread from this group will go to this method and you can do much more than logging in there.

    Anyway, logging and exception handling done right is most vital for stable programs. That is the reason why my blog http://goit-postal.blogspot.com speaks so often about that 😉

    Just my 0,02$, Georgi

  3. Xebia Blog says:

    […] in the example below, is something I do see regularly. Although the exception handling is sometimes forgotten. This Task is executed by some thread, and when a different thread wants to stop this Task, it […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: