On throwing InterruptedException

Remi Forax forax at univ-mlv.fr
Wed Nov 5 14:00:24 UTC 2025


----- Original Message -----
> From: "Pavel Rappo" <pavel.rappo at gmail.com>
> To: "core-libs-dev" <core-libs-dev at openjdk.org>
> Sent: Wednesday, November 5, 2025 12:10:11 PM
> Subject: On throwing InterruptedException

> I've seen code that wraps InterruptedException in some other exception
> prior to throwing that other exception; for example:
> 
>    catch (InterruptedException e) {
>        throw new IOException(e);
>    }
> 
> I wonder if there are any legitimate cases for this, because I cannot
> think of any. In my mind, it's always InterruptedException itself that
> should be thrown; and if it cannot be done, then the interrupted
> status should be set.
> 
> This is because if code that catches exceptions expects
> InterruptedException, it expects to catch it directly rather than
> search the exception graph (i.e. cause/suppressed) of the caught
> exception for InterruptedException.

It depends on whether you have faith in your fellow developers :)

If a thread is interrupted, it should stop ASAP.
So if you catch InterruptedException and rethrow a new exception, this should be okay (as Alan said).

But in practice, it may be a bad idea because people tend to write code that catch exceptions and do nothing,
so throwing an IOError is usually safer.

> 
> -Pavel

regards,
Rémi


More information about the core-libs-dev mailing list