Artem's blog

Thoughts on software

What to do with InterruptedException in Java

Let’s discuss what you can do about annoying InterruptedException. In many situations you’d have a lot of methods like someOtherMethod() below, where you’d want to preserve it’s signature clear. Or, you call goes through 3-rd party interfaces which do not throw InterruptedException. What do you do?

First of all you have to know what kind of application do you write:

  1. Web application like Servlet or Struts controller;
  2. EBJ bean;
  3. Applet;
  4. Desktop or mobile rich application;
  5. Single-threaded console application;
  6. Multi-threaded console application;

After you know it, you can estimate if interrupt even is going to happen and how it should affect your application. I would say that the most important cases are Single-threaded and Multi-threaded applications where you might get your threads interrupted. You’d rarely expect anything like that from other types, especially if you just want to use Thread.sleep().

Than you should decide if your application would be interruptable. I would only consider interruptable case here. Non-interruptable case is clearly described here.

In case of Single-threaded application you’d probably want to do something to address it and it is more or less straight-forward, in multi-threaded application you’d probably have some monitoring thread which would eventually check health status of applications and would do something about it (like you might want to start a new thread or shutdown the whole application in case of on of thread being interrupted), but still you might want to do some clean-up on lowest level.

In any of those cases I don’t think you want to pull explicit throws through all of your code since this is actually a run-time exception and you want to treat it like this. Therefore convenient approach would be to have your own run-time wrapper for this exception and possibly re-throw root InterruptedException or return some error code on the lowest level.

Let me show here what I am talking about. I would have a wrapper like this:

 Java |  copy code |? 
01
02
public class InterruptedRuntimeException extends RuntimeException {
03
	private static final long serialVersionUID = 6963102177803249360L;
04
 
05
	public InterruptedException getCauseInterruptedException() {
06
		return (InterruptedException) this.getCause();
07
	}
08
 
09
	public InterruptedRuntimeException(String message, InterruptedException cause) {
10
		super(message, cause);
11
	}
12
 
13
	public InterruptedRuntimeException(InterruptedException cause) {
14
		super(cause);
15
	}
16
}
17

Which would work this way:

 Java |  copy code |? 
01
02
public class ExceptionHandling {
03
 
04
	public static int main(String[] args) {
05
		try {
06
			someOtherMethod();
07
		} catch (InterruptedRuntimeException e) {
08
			return 2;
09
		}
10
 
11
		return 0;
12
	}
13
 
14
	private static void someOtherMethod() {
15
		throwingMethod();
16
	}
17
 
18
	private static void throwingMethod() {
19
		try {
20
			Thread.sleep(100);
21
		} catch (InterruptedException e) {
22
			Thread.currentThread().interrupt();
23
			throw new InterruptedRuntimeException("Your Message", e);
24
		}
25
	}
26
}
27

For a single threaded application.

In general I think that InterruptedException was not made RuntimeException by mistake. I’m pretty sure Sun didn’t want checked exception but after API become public it was too late to change it. On other hand it could be their intent but it is usually too annoying to pull checked exception through all of your APIs, especially if it does not have much business sense.

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>