[英]How does this try-catch works?
我正在閱讀的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.");
}
}
}
我沒有得到的是"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.");
}
}
}
}
catch 捕獲NumberFormatException
,而不是IllegalArgumentException
,因此它不會捕獲throw
語句中拋出的異常。 NumberFormatException
繼承自IllegalArgumentException
的事實並不重要。 catch
子句在inheritance 樹中捕獲異常,而不是向上。 如果一個人可以接住所有的Tennisball
,而你向他們扔一個普通的Ball
,他們不會接住它,不是嗎?
假設try
子句中的其他方法沒有拋出NumberFormatException
,那么catch
子句旨在捕獲Integer.parseInt
拋出的NumberFormatException
。
所以我想知道是否可以按頻率打印協調員工作......日志。
它可以由另一種方法打印出來,該方法在調用堆棧中進一步捕獲異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.