作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在第一个代码 (code1) 中,findbugs 发现了一个 REC_catch_Exception 警告,因为
try { ... } catch (Exception e)
不是一个好的风格。但是在第二个代码 (code2) 中警告消失了。
为什么?唯一的区别是 setMatrikelnummer
采用的类型:Integer
与 String
。
//code1: With REC_Catch_Exception
try {
// set student datas
currentStudent.setVorname(registration[0]);
currentStudent.setName(registration[1]);
currentStudent.setMatrikelnummer(Integer
.parseInt(registration[2]));
currentStudent.setEmail(registration[3]);
currentStudent.setAnrede(registration[4]);
currentStudent.setStudiengang(registration[5]);
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss z yyyy", Locale.UK);
Date registrationDate = formatter.parse(registration[6]);
currentRegistration.setRegistrationDate(registrationDate);
} catch (Exception E) {
throw new WrongFormatException(
"Die Textdateien befinden sich im falschen Format");
}
//code2: Without REC_Catch_Exception
try {
// set student datas
currentStudent.setVorname(registration[0]);
currentStudent.setName(registration[1]);
currentStudent.setMatrikelnummer(registration[2]);
currentStudent.setEmail(registration[3]);
currentStudent.setAnrede(registration[4]);
currentStudent.setStudiengang(registration[5]);
DateFormat formatter = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss z yyyy", Locale.UK);
Date registrationDate = formatter.parse(registration[6]);
currentRegistration.setRegistrationDate(registrationDate);
} catch (Exception E) {
throw new WrongFormatException(
"Die Textdateien befinden sich im falschen Format");
}
最佳答案
当try-catch block 中出现多个异常时,REC_Catch_Exception触发。也许在第一个代码中,可能有两个异常并触发 REC_Catch_Exception,但在第二个代码中,可能只有一个异常并且没有 REC_Catch_Exception 触发器/
关于java - 为什么在一个版本中使用 REC_Catch_Exception 而在另一个版本中不使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968959/
在第一个代码 (code1) 中,findbugs 发现了一个 REC_catch_Exception 警告,因为 try { ... } catch (Exception e) 不是一个好的风格。但
我是一名优秀的程序员,十分优秀!