Consider I scheduled a Runnable for periodic execution with ScheduledExecutorService and there occurs some system Error like OutOfMemory. It will be silently swallowed.
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
throw new OutOfMemoryError(); // Swallowed
}
}, 0, delay, TimeUnit.SECONDS);
Is it normal?
Why doesn’t it propagate to the container?
What is the correct way to handle such errors?
Thanks!
An
OutOfMemoryError, like any subtype ofError, cannot be recovered from, they’re generally fatal to your program. It doesn’t really matter if you try and catch it or not, your program’s going down.If, however, you mean an
Exceptionrather than anError, then you have 2 options:run()method.get()on theFuture()returned byscheduleWithFixedDelay(). This will propagate the exception back to the submitting thread, but it will block until that happens.