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.

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

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

  • Java Platform, Standard Edition 8, API Specification: Integer.valueOf, Integer.parseInt, Long.parseLong, NumberFormatException.