I have the following code:
public boolean perform(CronJob job)
final Date lastQueryDateTime = new Date();
QueryMode queryMode = job.getQueryModeEnum();
try {
if (queryMode.getCode().equals("FULL"))
{
calculateCoverage();
} else if (queryMode.getCode().equals("INCREMENTAL"))
{
calculateCoverage();
}
job.setLastQueryDate(lastQueryDateTime);
job.save(job);
return true;
} catch (Exception e) {
LOG.error("Error while calculating the coverage for products.", e);
return false;
}
}
private void calculateCoverage()
{
// Some Logic
}
I got in doubt if there is actually too much logic to be handled in between and if the try/catch scope is to wide. Is there a better approach to do this??. I would liket handle the try/catch within calculateCoverage but in that case I will need to pass job as an argurment to calculateCoverage and I think is not that clean.. Any suggestions?
thank you!
It looks like this is a Quartz job (or something like it), which isn’t supposed to allow an exception to be thrown.
So it’s not that you expect a particular method to throw a particular type of exception (in which case you would want to reduce the scope to that method call). Rather, you want to ensure that if anything inside this method goes awry, you won’t put your Cron Job into an error state.
Since the sole purpose of the
try/catchin this case is to log any exceptions that are thrown, and you’re not planning to handle them in any special way, I think it’s perfectly reasonable to have the block encompass the entire contents of the method.Pay attention to the overall logic, though. Currently an exception in calculating coverage will result in the job not being set to run again. If you want the job to run later even if it fails now, you may want to have a try/finally block in there.
This will still cause any exception in the method to be caught and logged, but if the error happens while calculating coverage, you’ll still try to set the job to run again.