虽然无法到达的 catch 部分通常不被 Java 编译器允许,并且会被报告为编译错误,但在某些情况下,Java 语言规范所要求的分析并不完整。 此检查提供了增强的分析,并会报告一些编译器没有报告的无法到达的 catch 部分。 这些部分是冗余的,可以安全移除。
示例:
void method() {
try {
throw new FileNotFoundException();
}
catch (FileNotFoundException e) {
}
catch (IOException e) {
// 规范允许此 catch
// 但从未被执行
}
}
提供了快速修复,可移除冗余的 catch 部分:
void method() {
try {
throw new FileNotFoundException();
}
catch (FileNotFoundException e) {
}
}
2025.1 的新功能