【发布时间】:2019-08-27 11:26:24
【问题描述】:
我有以下代码,我正在通过 fortify 运行。为什么它被标记为糟糕的错误处理,最后扔进去?
private String getResourceContent(String fileName) throws IOException {
try (InputStream resource = ErrorResource.classLoader.getResourceAsStream(fileName)) {
return new String(resource.readAllBytes(), StandardCharsets.UTF_8);
} catch (NullPointerException n) {
throw new ErrorDescriptorException(
String.format("Error loading Error description data from Resource file [%s].", fileName), n);
}
}
【问题讨论】:
-
你为什么要使用 NPE 而不是基本的 if-else 检查
null?很少有正当的理由抓住 NPE。只需if (resource == null) { ... }。