NumberFormatException in java


What is hierarchy of java.lang.NumberFormatException?

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

NumberFormatException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.NumberFormatException is a RuntimeExceptions in java.


What is NumberFormatException in java?

java.lang.NumberFormatException is thrown when program attempts to convert a string to numeric types, but that the string is not in the appropriate format.

Scenarios where NumberFormatException may be thrown in java>

String "22" is in appropriate format to be converted into numeric type int.  So, NumberFormatException will not be thrown.

public class NumberFormatExceptionExample {
public static void main(String args[]) {
     String s = "22";
     int i = Integer.parseInt(s);
     System.out.println(i);
}
}

/*OUTPUT
22
*/

BUT, In the below program, String "ab" is not in appropriate format to be converted into numeric type int. So, NumberFormatException will be thrown.

public class NumberFormatExceptionExample2 {
public static void main(String args[]) {
     String s = "ab";
     int i = Integer.parseInt(s);
     System.out.println(i);
}
}

/*OUTPUT
Exception in thread "main" java.lang.NumberFormatException: For input string: "ab"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at  NumberFormatExceptionExample2.main(NumberFormatExceptionExample2.java:4)
*/


eEdit
Must read for you :