Advertisement
Xetos

Java exception

Apr 14th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. http://beginnersbook.com/2013/04/exception-handling-examples/
  2.  
  3. Example 3: NumberFormat Exception
  4.  
  5. Class: Java.lang.NumberFormatException
  6.  
  7. The object of the above built-in class gets created whenever a string is parsed to any numeric variable.
  8. For E.g. The statement int num=Integer.parseInt ("XYZ") ; would throw NumberFormatException because String “XYZ” cannot be parsed to int.
  9.  
  10. Complete Code:
  11.  
  12. class ExceptionDemo3
  13. {
  14.    public static void main(String args[])
  15.    {
  16.       try{
  17.      int num=Integer.parseInt ("XYZ") ;
  18.      System.out.println(num);
  19.       }catch(NumberFormatException e){
  20.       System.out.println("Number format exception occurred");
  21.        }
  22.    }
  23. }
  24.  
  25. Output:
  26.  
  27. Number format exception occurred
  28.  
  29.  
  30.  
  31.  
  32. class ExceptionDemo3
  33. {
  34.    public static void main(String args[])
  35.    {
  36.       try{
  37.      int num=Integer.parseInt ("XYZ") ;
  38.      System.out.println(num);
  39.       }catch(NumberFormatException e){
  40.       System.out.println("Number format exception occurred");
  41.        }
  42.    }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement