Reputation: 1405
The Java code I'm reading is:
private void validateCoordinatorJob() throws Exception {
// check if startTime < endTime
if (!coordJob.getStartTime().before(coordJob.getEndTime())) {
throw new IllegalArgumentException("Coordinator Start Time must be earlier than End Time.");
}
try {
// Check if a coord job with cron frequency will materialize actions
int freq = Integer.parseInt(coordJob.getFrequency());
// Check if the frequency is faster than 5 min if enabled
if (ConfigurationService.getBoolean(CONF_CHECK_MAX_FREQUENCY)) {
CoordinatorJob.Timeunit unit = coordJob.getTimeUnit();
if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE)) {
throw new IllegalArgumentException("Coordinator job with frequency [" + freq +
"] minutes is faster than allowed maximum of 5 minutes ("
+ CONF_CHECK_MAX_FREQUENCY + " is set to true)");
}
}
} catch (NumberFormatException e) {
Date start = coordJob.getStartTime();
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, -1);
start = cal.getTime();
Date nextTime = CoordCommandUtils.getNextValidActionTimeForCronFrequency(start, coordJob);
if (nextTime == null) {
throw new IllegalArgumentException("Invalid coordinator cron frequency: " + coordJob.getFrequency());
}
if (!nextTime.before(coordJob.getEndTime())) {
throw new IllegalArgumentException("Coordinator job with frequency '" +
coordJob.getFrequency() + "' materializes no actions between start and end time.");
}
}
}
What I don't get is whether "Coordinator job with frequency [" + freq + "] minutes is faster than allowed maximum of 5 minutes ("+ CONF_CHECK_MAX_FREQUENCY + " is set to true)"
log can be printed.
As long as I know, when if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
condition meets and the program throws IllegalArgumentException.
The thrown exception will be caught at catch (NumberFormatException e)
, and handled in that block as e
.
But that e
is never used afterward.
So I wonder whether it's possible to print Coordinator job with frequency ...
log.
For me, it doesn't seem to have a difference between the original code and just putting the catch block into if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
statement, as below:
private void validateCoordinatorJob() throws Exception {
// check if startTime < endTime
if (!coordJob.getStartTime().before(coordJob.getEndTime())) {
throw new IllegalArgumentException("Coordinator Start Time must be earlier than End Time.");
}
// Check if a coord job with cron frequency will materialize actions
int freq = Integer.parseInt(coordJob.getFrequency());
// Check if the frequency is faster than 5 min if enabled
if (ConfigurationService.getBoolean(CONF_CHECK_MAX_FREQUENCY)) {
CoordinatorJob.Timeunit unit = coordJob.getTimeUnit();
if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE)) {
Date start = coordJob.getStartTime();
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.MINUTE, -1);
start = cal.getTime();
Date nextTime = CoordCommandUtils.getNextValidActionTimeForCronFrequency(start, coordJob);
if (nextTime == null) {
throw new IllegalArgumentException("Invalid coordinator cron frequency: " + coordJob.getFrequency());
}
if (!nextTime.before(coordJob.getEndTime())) {
throw new IllegalArgumentException("Coordinator job with frequency '" +
coordJob.getFrequency() + "' materializes no actions between start and end time.");
}
}
}
}
Upvotes: 0
Views: 74
Reputation: 274835
The catch catches a NumberFormatException
, not an IllegalArgumentException
, so it won't catch the the exception thrown in the throw
statement. The fact that NumberFormatException
inherits from IllegalArgumentException
doesn't matter. catch
clauses catch exceptions down the inheritance tree, not up. If a person can catch all Tennisball
s, and you throw a general Ball
at them, they would not catch it, would they?
Assuming that the other methods in the try
clause doesn't throw NumberFormatException
s, the catch
clause is meant to catch the NumberFormatException
thrown by Integer.parseInt
.
So I wonder whether it's possible to print Coordinator job with frequency ... log.
It could be printed by another method that catches the exception further up the call stack.
Upvotes: 2