Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - NumberFormatException



The Java NumberFormatException is thrown when a method that converts a string to a number encounters a string that cannot be converted to a number. The NumberFormatException is a checked exception that is thrown when a method that converts a string to a number encounters a string that cannot be converted to a number.

It extends IllegalArgumentException, which is the super-class for exceptions thrown when a method receives an illegal or inappropriate argument.

Hierarchy of NumberFormatException

Given below is the class hierarchy of NumberFormatException:

java.lang.Object
  ↳ java.lang.Throwable
      ↳ java.lang.Exception
          ↳  java.lang.RuntimeException
              ↳ java.lang.IllegalArgumentException
                  ↳ java.lang.NumberFormatException

So, it means IllegalArgumentException is the superclass for exceptions thrown when a method receives an illegal or inappropriate argument and it is unchecked.

Causes of NumberFormatException

Following are the reasons when JVM throws a NumberFormatException in Java:

  • When a method that converts a string to a number encounters a string that cannot be converted to a number, JVM throws a NumberFormatException.

Constructors of NumberFormatException

There are two constructors of NumberFormatException class:

  • NumberFormatException(): This constructor is used to create a NumberFormatException object without any message.
  • NumberFormatException(String message): This constructor is used to create a NumberFormatException object with a message.

Methods of NumberFormatException

There are some methods of NumberFormatException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.

Example of NumberFormatException

In this example, we are trying to convert a string to a number, but the string is not a number, so JVM will throw a NumberFormatException.

public class NumberFormatExceptionExample {

   public static void main(String[] args) {
      String str = "abc";
      int num = Integer.parseInt(str); // This will throw NumberFormatException
   }
}

Output

Following is the output of the above code:

Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:6)

As you can see in the output, JVM throws a NumberFormatException because we are trying to convert a string to a number, but the string is not a number.

Handling NumberFormatException

In this example, we are trying to convert a string to a number, but the string is not a number, so JVM will throw a NumberFormatException. We are handling this exception using try-catch block.

public class NumberFormatExceptionExample {

   public static void main(String[] args) {
      String str = "abc";
      try {
         int num = Integer.parseInt(str); // This will throw NumberFormatException
      } catch(NumberFormatException e) {
         System.out.println("Caught a NumberFormatException: Can't convert a string to a number");
      }
   }
}

Output

Following is the output of the above code:

Caught a NumberFormatException: Can't convert a string to a number

How to avoid NumberFormatException?

Following are the ways to avoid NumberFormatException in Java:

  • Always use try-catch block while converting a string to a number.
  • Always check the string before converting it to a number.
java_lang_exceptions.htm
Advertisements