Java – how to reschedule tasks using scheduledexecutorservice?
I saw this in Java docs: scheduledatfixedrate
I don't want this to happen in my application Even if I see an exception, I always want the subsequent execution to happen and continue How to get this behavior from scheduledexecutorservice
Solution
Use try / catch to surround callable Call method or runnable Run method
For example:
public void run() { try { // ... code } catch(final IOException ex) { // handle it } catch(final RuntimeException ex) { // handle it } catch(final Exception ex) { // handle it } catch(final Error ex) { // handle it } catch(final Throwable ex) { // handle it } }
Note that capturing anything other than what the compiler tells you (IOException in my example) is not a good idea, but sometimes it sounds like one of them. If you handle it correctly, it can be solved
Remember, things like errors are very bad - virtual machines are out of memory, etc., so be careful how you deal with them (that's why I separate them into my own handlers instead of just catching (the last throwable Ex) and nothing else)