I've been learning about concurrency and the streams API
and came across this. The offerLast()
method can throw InterruptedException
, so I get that I must handle it. What I don't get is why can't I throw it at the method level by adding throws Exception
?. As it is this code
does not compile.
static BlockingDeque<Integer> queue = new LinkedBlockingDeque<>();
public static void testing() throws Exception {
IntStream.iterate(1, i -> i+1).limit(5)
.parallel()
.forEach(s -> queue.offerLast(s, 10000, TimeUnit.MILLISECONDS));
}
I know it can be solved by surrounding it in a try/catch
, or by creating a wrapper method
that handles the error
, but I'm still trying to understand why it can't be thrown at the method level.