gpt4 book ai didi

java - 为什么在一个版本中使用 REC_Catch_Exception 而在另一个版本中不使用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:42 25 4
gpt4 key购买 nike

在第一个代码 (code1) 中,findbugs 发现了一个 REC_catch_Exception 警告,因为

try { ... } catch (Exception e) 

不是一个好的风格。但是在第二个代码 (code2) 中警告消失了。

为什么?唯一的区别是 setMatrikelnummer 采用的类型:IntegerString

        //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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com