Damn checked Interrupted Exception

A lot has been written about checked and unchecked exceptions and in most cases I prefer using unchecked ones. Because you don’t want deal with the exception, but want it to be propagated instead, so it can be handled higher up the call stack. In case of pooled threads, like in a servlet container or Executor, the exception is caught and usually logged and the thread is made available for the next task.

InterruptedExceptions are no difference: in most cases the thread container interrupts blocking threads (because it wants to shut down for example) and only this container is able to deal with the InterruptedException. This means that you are forced to catch it, but normally this is not something you want because an interrupted thread should ‘stop’ as fast as possible to return to the top of the callstack so the threadcontainer can decide what to do.

A better solution is to propagate the exception, but because the InterruptedException is checked, it also need to be added to the throws clause. The problem with this solution is that you keep adding it to the throws clauses higher up the callstack and this isn’t always the cleanest solution or even possible. So in both cases, you are screwed.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: