My existing code has a scheduler that we init
when a DataStore constructor is called.
private void initSchedulers() {
int initialDelay = applicationStartupConfig.getSchedulerInitialDelay();
int refreshPeriod = applicationStartupConfig.getSchedulerRefreshPeriod();
// above are fetched using config.yml(dropwizard)
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
initializeCache(); // some operation that we perform to fetch data on to the cache on a scheduled basis
} catch (Throwable t) {
logger.error("Scheduler could not complete : ", t);
}
}
}, initialDelay, refreshPeriod);
}
Evaluating the code under Sonar Analysis reports this as Vulnerable --
Throwable is the superclass of all errors and exceptions in Java.
Error is the superclass of all errors, which are not meant to be caught by applications.
Catching either Throwable or Error will also catch OutOfMemoryError and InternalError, from which an application should not attempt to recover.
Though I understand the above statements yet I am not able to draw a conclusion on
- What if I catch
Exception
today, and a runtime error exception is thrown later? Would that not stop the scheduler? - Is there a way to specify handling both these cases and ensuring that this does not create impact on client calls to the DataStore?