Trifork Blog

What to do with an Interruptedexception

October 5th, 2007 by

Many java methods, such as Thread.sleep and Process.waitFor can throw an Interruptedexception. InterruptedException is a checked exception and as such requires you to explicitly catch it. Up until recently I’ve always simply swallowed this exception. I was fairly confident that this was the proper way to deal with this exception because this is how I’d seen others handle it many times over. As it turns out I was wrong and this is the worst possible way to handle an interrupted exception

Brian Goetz of java concurrency in practice fame has written an article on the subject that is definitely worth a read

If there is anything you should take away from reading this article it is this :

“At the very least, whenever you catch InterruptedException and don’t rethrow it, reinterrupt the current thread before returning.”

Its interesting to see that even the spring guys initially got this wrong It was fixed in spring 1.2.8

One Response

  1. October 9, 2007 at 22:37 by Alef Arendsen

    I saw a wonderful presentation by Heinz Kabuts last weekend that detailed this very approach. This was in Greece and he had his presentation titled ‘Top 10 laws of concurrency’. It also including the Law of Greek Driving, the Law of the Distracted Spear Fisherman’ and so on. If you ever get a chance to see this talk, please do so, it’s great :).