CodeQL 文档

缺少 NumberFormatException 的 catch

ID: java/uncaught-number-format-exception
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - reliability
   - external/cwe/cwe-248
Query suites:
   - java-security-and-quality.qls

单击以在 CodeQL 存储库中查看查询

诸如 Integer.parseInt(将字符串解析为数字)之类的解析字符串的方法如果其参数无法解析,将抛出 NumberFormatException。应捕获此异常,以便处理任何解析错误。

建议

通常最好在围绕解析方法调用的 catch 子句中处理 NumberFormatException

示例

在以下示例中,对 Integer.parseInt 的第一次调用未捕获异常。第二次调用捕获了异常。

String s = ...;
int n;

n = Integer.parseInt(s); // BAD: NumberFormatException is not caught.

try {
        n = Integer.parseInt(s);
} catch (NumberFormatException e) {  // GOOD: The exception is caught. 
        // Handle the exception
}

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私