NumberFormatException is an exception which might be thrown when a String is converted into a Number.

int x = Integer.parseInt(“1267”); // Valid
int parseInt = Integer.parseInt(“abc”); // throws NumberFormatException

int parseInt = Integer.parseInt(“abc”); will throw exception as when “abc” is converted into Number, it is found that in actual “abc” is not a number.

NumberFormatException is Checked or Unchecked Exception??

NumberFormatException is a Runtime exception as it extends RuntimeException. RuntimeException and all subclasses of RuntimeException are unchecked exception.
Hence, NumberFormatException is an unchecked exception.

Unchecked exception means exception need not be caught in try….catch black or throws at method level. Compiler allows to compile the code without them. It is thrown at run time if this exception occurs.

Some examples where NumberFormatException can occur:

The below are some of the cases where NumberFormatException will be thrown.

(I.) Integer.parseInt(“123”); //Valid
Integer.parseInt(“12SMDHGJD3”); // throws exception

(II.)Long.parseLong(“726”); //Valid
Long.parseLong(“msdjh6523”); // throws exception

(III.)Double.parseDouble(“1272”); //Valid
Double.parseDouble(“12enhgdhs”); // throws exception

Demo Examples

package com.masterjavatutorial;
public class TestExample {
	public static void main(String[] args){
		 long parseLong1 = Long.parseLong("6523"); 
		 long parseLong = Long.parseLong("msdjh6523"); 

Output of the above program is as below:

Exception in thread "main" java.lang.NumberFormatException: For input string: "msdjh6523"
	at java.lang.NumberFormatException.forInputString(
	at java.lang.Long.parseLong(
	at java.lang.Long.parseLong(
	at com.masterjavatutorial.TestExample.main(