NumberFormatException in Java
Overview
NumberFormatException is an unchecked exception, a subclass of IllegalArgumentException. In this class, a serializable interface is implemented. It occurs while converting a String with the improper format to other numeric forms like Integer, or float.
Introduction to NumberFormatException in Java
It is thrown while converting a string of improper format to a numeric value like an integer or float. An example of improper format can be a string that has a boolean value. If this string is converted to a numeric value, then an exception is thrown. For instance, parsing a string that is null to an integer. It is an unchecked exception, also known as a run time exception. The try-catch block is used for handling this exception. When operating with the string, a parseInt() method is used for converting a string to an integer type. parseInt() method can be used in two ways as given below:
Both methods are used for converting a string to an integer. The only difference between these two syntaxes is the parameter radix. When radix=10, the first method is considered equivalent to that of the second method.
Constructors of NumberFormatException
Constructor | Description |
---|---|
public NumberFormatException() | default constructor, an exception, NumberFormatException with no description of it. |
public NumberFormatException(String msg) | Constructs an exception, NumberFormatException with a detailed description of it, parameterized, with an error message . |
What Causes NumberFormatException?
There are various problems related to formatting string which is improper. Here are a few important reasons for NumberFormatException:
- Null input string: Integer.parseInt("null");
- Input string which is empty: Float.parseFloat(“”);
- Leading or trailing white spaces in input string: Integer abc=new Integer(“ 123 “);
- Extra symbol in input string: Float.parseFloat(4,123);
- Input string with data which is non-numeric: Double.parseDouble(“FifityFive”);
- Alphanumeric input string: Integer.valueOf(“33.three”);
- Input string with exceeded range of datatype: Integer.parseInt(“1826589745243”);
- Type mismatch between the value of input string and its method: Integer.parseInt("12.33");
Example of NumberFormatException
Program to illustrate NumberFormatException in java. In this example, we will try to understand how and why NumberFormartException occurs.
Output:
Explanation: As per code, as soon as the user enters a number other than Integer like double or float then the NumberFormatException occurs.
Flow of the given code:
- Input is taken from user
- The input is parsed using parseInt
- If there is an error while parsing
- NumberFormatException is thrown
- Else Output is printed
How to Handle NumberFormatException?
The NumberFormatException in java can be handled in two ways:
- Using the block try-catch : This involves surrounding the code that might cause the exception with a try block and handling the exception in a catch block. For example:
Output:
- Using throws keyword : This involves declaring that a method might throw a specific exception and leaving it to the calling method to handle it. For example, a method that parses a string to an integer might declare that it throws a NumberFormatException if the string is not a valid integer. The calling method would then have to handle this exception using a try-catch block or declare that it throws the exception using the throws keyword.
For example:
Output:
Conclusion
- NumberFormatException is thrown while converting a string of improper format to a numeric value like an integer or float. An example of improper format can be a string that has a boolean value. If this string is converted to a numeric value, then an exception is thrown.
- It is an unchecked exception, also known as a run time exception. The try-catch block is used for handling this exception.
- Constructors of NumberFormatException:
- public NumberFormatException()
- public NumberFormatException(String msg)
- The NumberFormatException in java can be handled in two ways:
- Using the block try-catch in the program or around the code that can cause this exception.
- Using throws keyword