On throwing InterruptedException
Alan Bateman
alan.bateman at oracle.com
Wed Nov 5 11:41:18 UTC 2025
On 05/11/2025 11:10, Pavel Rappo wrote:
> 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.
>
Right, if you catch InterruptedException then you should re-throw it, or
reset the interrupted status if continuing or throwing a different
exception. As it happens, I am hoping to adding "Thread Interruption"
section to the Thread class description that includes guidance on this,
and also gives us a place to link to from other parts of the API docs.
Work in progress [1], not a PR as it's ready for review.
-Alan
[1]
https://github.com/openjdk/jdk/compare/master...AlanBateman:jdk:JDK-8371226
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20251105/983b05c7/attachment.htm>
More information about the core-libs-dev
mailing list