Why is InterruptedException a checked exception?

When working with threads in Java, handling InterruptedException seems to be a special thorn in my direction. I appreciate the fact that it was thrown when my flows were stopped, and thus gives me the opportunity to clear. It seems strange to me that this is no exception.

This creates the following problems: a) If I want to use the existing infrastructure in my streaming application, I have to convert it to an exception that the framework interface accepts. Thus, a structure usually misinterprets it, rather than cleansing or distributing it as it should.

b) If an InterruptedException is not strictly declared for every call on the stack (and usually not because of a)), this makes it difficult to cleanly shut down.

If InterruptedException were disabled, it seems that it will have a much more likely hood for proper use, which will lead to a clean disconnection of threads and applications in general. Why is this not so?

+6
source share
1 answer

The interruption is supposed to be joint. I think that the designers wanted to avoid a situation where you could blow off the thread by interrupting it, where the thread had no code to handle this accident. The goal was supposed to make Runnable code explicitly decide how to handle interrupts. A lot of frameworks or language code seems to be related to deciding whose responsibility it should be and trying to make proper use obvious to minimize how much users are burned. This is one of these solutions.

When checking for InterruptedException, the worst case is that the exception is caught, but not very useful. If it is not set, the exception may go away without processing and continue to terminate the thread, which can be very bad if the thread is not at a stop.

It is also obvious that designers tend to err on the side of checking things. Thus, this would be consistent with this trend.

+3
source

Source: https://habr.com/ru/post/982635/


All Articles