In Java, Thread.sleep() throws InterruptedException. What is the proper thing to do with this exception?
- propagate the exception as a failure up the call chain
- eat the exception and proceed with your code
- other
In Java, Thread.sleep() throws InterruptedException. What is the proper thing to do with this exception?
You should rethrow the exception.
This is an abstract from Brian Goetz, the author of the excellent book "Java Concurrency in Practice":
Dealing with InterruptedException:
If throwing InterruptedException means that a method is a blocking method, then calling a blocking method means that your method is a blocking method too, and you should have a strategy for dealing with InterruptedException. Often the easiest strategy is to throw InterruptedException yourself, as shown in the putTask() and getTask() methods in Listing 1. Doing so makes your method responsive to interruption as well and often requires nothing more than adding InterruptedException to your throws clause.
If all you're trying to do is burn SOME time, I would suggest just catching and ignoring the exception.
If, on the other hand, you need the proper amount of waiting time and any preemptive interruption is REALLY BAD for your program, then you should definitely rethrow the exception.
It just depends on why the exception occurred and what your use case is for Thread.sleep()
in the first place.