java这个trycatch是如何工作的?
我正在读的Java code是:
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.");
}
}
}
我不明白的是"Coordinator job with frequency [" + freq + "] minutes is faster than allowed maximum of 5 minutes ("+ CONF_CHECK_MAX_FREQUENCY + " is set to true)"
日志是否可以打印出来
据我所知,当if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
条件满足并且程序抛出IllegalArgumentException时。
抛出的异常将在catch (NumberFormatException e)
处被捕获,并在该块中作为e
处理
但是e
之后就再也不用了
所以我想知道是否可以打印Coordinator job with frequency ...
日志
对我来说,原始代码和将catch块放入if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
语句之间似乎没有区别,如下所示:
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.");
}
}
}
}
# 1 楼答案
catch捕获的是
NumberFormatException
,而不是IllegalArgumentException
,因此它不会捕获throw
语句中抛出的异常。NumberFormatException
从IllegalArgumentException
继承的事实并不重要catch
子句捕获继承树下的异常,而不是上的异常。如果一个人能抓住所有的Tennisball
,而你向他们扔一个Ball
将军,他们不会抓住的,是吗假设
try
子句中的其他方法不抛出NumberFormatException
,那么catch
子句将捕获Integer.parseInt
抛出的NumberFormatException
它可以通过另一种方法打印出来,该方法可以捕获调用堆栈中更上层的异常