Friday, November 25, 2011

NumberFormatException


packageorg.training.exceptions;

public class NumberFormat {

      public static void main(String[] args) {
           
            String string = "hello";
           
            try {
                  // this parsing doesn't work as hello is not a number
                  Integer integer = Integer.parseInt(string);
                  System.out.println(integer++);
            }catch(NumberFormatException nfe) {
                  System.out.println("Number Format Exception caught, check what you are parsing");
                  nfe.printStackTrace();
            }
}


Output:

Number Format Exception caught, check what you are parsing
java.lang.NumberFormatException: For input string: "hello"
      at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at org.training.exceptions.NumberFormat.main(NumberFormat.java:11)

No comments:

Post a Comment