
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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.