[英]How does this try-catch works?
The Java code I'm reading is:我正在阅读的Java 代码是:
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.我没有得到的是
"Coordinator job with frequency [" + freq + "] minutes is faster than allowed maximum of 5 minutes ("+ CONF_CHECK_MAX_FREQUENCY + " is set to true)"
日志是否可以打印。
As long as I know, when if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
condition meets and the program throws IllegalArgumentException.据我所知,当
if (freq == 0 || (freq < 5 && unit == CoordinatorJob.Timeunit.MINUTE))
条件满足并且程序抛出 IllegalArgumentException 时。 The thrown exception will be caught at catch (NumberFormatException e)
, and handled in that block as e
.抛出的异常将在
catch (NumberFormatException e)
处被捕获,并在该块中作为e
处理。
But that e
is never used afterward.但是那个
e
之后再也没有使用过。
So I wonder whether it's possible to print Coordinator job with frequency...
log.所以我想知道是否可以按
Coordinator job with frequency...
日志。
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:对我来说,原始代码与将 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.");
}
}
}
}
The catch catches a NumberFormatException
, not an IllegalArgumentException
, so it won't catch the the exception thrown in the throw
statement. catch 捕获
NumberFormatException
,而不是IllegalArgumentException
,因此它不会捕获throw
语句中抛出的异常。 The fact that NumberFormatException
inherits from IllegalArgumentException
doesn't matter. NumberFormatException
继承自IllegalArgumentException
的事实并不重要。 catch
clauses catch exceptions down the inheritance tree, not up. catch
子句在inheritance 树中捕获异常,而不是向上。 If a person can catch all Tennisball
s, and you throw a general Ball
at them, they would not catch it, would they?如果一个人可以接住所有的
Tennisball
,而你向他们扔一个普通的Ball
,他们不会接住它,不是吗?
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
.假设
try
子句中的其他方法没有抛出NumberFormatException
,那么catch
子句旨在捕获Integer.parseInt
抛出的NumberFormatException
。
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.它可以由另一种方法打印出来,该方法在调用堆栈中进一步捕获异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.