1
在第一代碼(代碼1)的FindBugs找到REC_catch_Exception警告,因爲爲什麼在一個版本中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");
}