If I create my own thread (i.e. not a threadpool) and somewhere I call sleep
or any other interruptible method, is it ok to ignore the InterruptedException if I know nobody else in the code is doing an interrupt on the thread.
In other words, if the thread is supposed to live as long as the JVM, meaning the thread is not interruptible, is it safe to assume that InterruptedException will never be called and therefore the exception can be swallowed?
InterruptedExceptions should never be ignored in the code, and simply logging the exception counts in this case as "ignoring". The throwing of the InterruptedException clears the interrupted state of the Thread, so if the exception is not handled properly the information that the thread was interrupted will be lost.
If the blocking network call throws an InterruptedException your method can not finish computation in a normal way. You let the InterruptedException propagate. If no, then you should not declare your method with throws InterruptedException and you should (must!) catch the exception.
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
In Java Threads, if any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.
Ignoring a checked exception is never considered to be safe.
It may be ok for you at the moment, but if another programmer extends your code, he will expect the standard behaviour: the thread reacting to an interrupt call.
Also an empty catch block is dangerous in this case, since the JVM removes the interrupted flag and it should definitely be set again with
Thread.currentThread().interrupt();
in the catch block. In my opinion, this is the minimum catch implementation for InterruptedException
s. Checking for the isInterrupted
flag in a loop doesn't hurt much, either.
It is little overhead compared to your future programmer self's hassle searching a day or two for unexpected thread behaviour as you project may have grown a bit.
If you feel that your code's readability suffers from those catch implementations, you may implement your own safeSleep
utility method, which takes care of the Exception
s and sets the flag properly.
On the other hand, InterruptedException
is not thrown by the JVM itself in case of a hardware failure, it is a user indicated Exception
only. So, if you do not propagate your That's it technically. But you shouldn't underestimate the human factor and your programs evolution.Thread
s reference, there won't be any other Thread
s that are able to call Thread.interrupt()
on it.
Edit: As ruakh pointed out, there actually is a way to get a Thread
s reference and thus to schedule an Thread.interrupt()
call. That way the developer in heat may not even have to look at the class, that implements your uninterruptible Thread
. In my opinion that's even another reason, to implement proper exception handling.
Another thing: If you're not throwing an Exception
, logging such an event on a level beyond INFO
may be a good choice.
Instead of swallowing it, if you're so sure it will never happen, you can crash instead of ignoring it. For example, throw an Error
(or a RuntimeException
):
try { Thread.sleep(1000); } catch (InterruptedException e) { throw new Error(e); }
From the Javadocs for Error:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
If you think it's worth assuming something will never happen, then if it does happen, that's an "abnormal condition" that, who knows, might just be a "serious problem".
Another way to look at this is, if someone in the future does interrupt your method, is there any chance that they will want it to continue running as if nothing had happened? I would guess no.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With