GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Missing catch of NumberFormatException — CodeQL query help documentation

Missing catch of NumberFormatException¶

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

Click to see the query in the CodeQL repository

Methods such as Integer.parseInt that parse strings into numbers throw NumberFormatException if their arguments cannot be parsed. This exception should be caught so that any parse errors can be handled.

Recommendation¶

It is usually best to handle NumberFormatException in a catch clause surrounding the call to the parsing method.

Example¶

In the following example, the first call to Integer.parseInt does not catch the exception. The second call does.

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
}

References¶

  • Java API Specification: Integer.valueOf, Integer.parseInt, Long.parseLong, NumberFormatException.

  • Common Weakness Enumeration: CWE-248.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.