Top 5 java exception

As a java programmer we should know about most usual java exception. Here we figure out top 5 java exception.

  1. ArrayIndexOutOfBoundsException: 
  • It is RuntimeException so that it is unchecked exception. 
  • It occurs whenever we try to access Array element out of index range. 
  • It is automatically raised by JVM.   
Suppose that an array have 100 elements and we are trying to access 110th element then we
will get ArrayIndexOutOfBoundsException.
                                  
                                            int [] array = new int[100] ;
                                            System.out.println(array[2]); //accessible
                                            System.out.println(array[105]); //not accessible
                                           //it throw ArrayIndexOutOBoundsException

  2. NullPointerException:
  • It is also RuntimeException so that it is unchecked.
  • It occurs whenever we try to any operation on null value.
  • It is automatically generated by JVM so it is JVA type exception.
   Example :
   String s1 = null;
                            System.out.println(s1.size()); //throw null pointer exception

  3. ClassCastException: 
  • It is the child class of RuntimeException so that it is unchecked exception.
  • When we try to cast the parent object to child object then ClassCastException occurs.
  • Since it is RuntimeException so that it is throw by JVM.
    Object obj = new Object();
    String str = (String)obj; //throw ClassCastException

  4. IllegalArgumentException:
  • It is unchecked exception.
  • It is raised by programmer or any API
  • If a method have the invalid argument then it occurs.
 5. NumberFormatException:
  • It is child class of RuntimeException and so that it is unchecked exception.
  • It is raised by programmer explicitly 
  • When we try to convert String to Number type then it throw NumberFormatException.       

0 comments :

Post a Comment