Skip to main content

Why CancellationException is runtime exception?

No replies
jiangshachina
Offline
Joined: 2006-05-05
Points: 0

I get a Future instance via ExecutorService.submit(Callable task).
The task may be very time-consuming, so at some time, user may cancel the task by calling method Future.cancel() in another thread, e.g Swing EDT.
It's possible that no one knows the task is cancelled, so some codes still call method Future.get() to try to get the task result, then a CancellationException is thrown.
But unfortunately, the codes don't handle the exception, because it just be runtime exception, and Future.get() doesn't throw it.

Why CancellationException is not checked exception?
In fact, the other get method, Future.get(long timeout, TimeUnit unit), would throw TimeoutException, which is checked exception.

I think it's common case that a task is cancelled at runtime, so it's necessary to force application codes to handle the exception, or the application will doesn't work.