在第一个代码(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");
}
发布于 2014-07-29 18:16:11
当try-catch块中有多个异常时,REC_Catch_Exception触发。在第一个代码中,可能有两个异常是REC_Catch_Exception触发器,但在第二个代码中,只有一个异常是可能的,并且没有REC_Catch_Exception触发器/
https://stackoverflow.com/questions/24968959
复制相似问题