NumberFormatException in Java

What is NumberFormatException in Java ?

NumberFormatException is an unchecked/runtime exception which occurs when you attempt to convert a string to one of the numeric types, but that string does not have the appropriate format.

NumberFormatException is thrown to indicate that the application has attempted to Convert String to int where the String is not well formatted for the conversion. For ex. String “12B” is converted to int.

Let’s see NumberFormatException with an example.

NumberFormatException with an Example:

Output:

In the above program, we have tried to convert String “45C” to int. Since the String “45C” contains the character, the program has thrown NumberFormatException.

How to Resolve NumberFormatException in Java ?

The best way to handle/resolve a NumberFormatException is to use try catch block in Java. Let’s see with an example.

Output:

Related posts

Leave a Comment