On throwing InterruptedException
Pavel Rappo
pavel.rappo at gmail.com
Thu Nov 6 10:24:12 UTC 2025
For absolute clarity, I think Alan is talking about these:
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new InterruptedException();
}
In each of these, a single interruption is signalled twice. I think it
should be avoided as confusing. But like Alan said, in effect it's no
different from concurrent interrupts.
FWIW, I think robust code should be idempotent in respect to
interruption. It's not a three-strike system or some such. A single
interrupt should (eventually) have the same effect as any number of
them.
On Thu, Nov 6, 2025 at 9:34 AM Alan Bateman <alan.bateman at oracle.com> wrote:
>
>
>
> On 05/11/2025 23:10, Pavel Rappo wrote:
>
> And also this, which is a bit contrived but valid nonetheless:
>
> } catch (InterruptedException e) { // good
> throw new InterruptedException();
>
> }
>
> The good/bad table is fun. For completeness you can also add the case where someone restores the interrupt status and rethrows the InterruptedException (or throws a new InterruptedException). It would be strange to do this but no different to a double whammy where two parts of the system are requesting you to cancel/finish-up around the same time.
>
> -Alan
More information about the core-libs-dev
mailing list