Top 10 exceptions in java

 Top 10 exceptions in java

Based on the person who is raising an exception all exceptions are divided into 2 category.
 
  1. JVM Exception
  2. Programmatic Exception

JVM Exception: 

The exception which are raised automatically by JVM whenever a particular event occurs are called JVM exception. 
 
Ex: ArithmeticException, NullPointerException, ...... 

Programmatic Exception:

The exception which are raised explicitly either by programmer or by API developer to indicate that something goes wrong are called programmatic exception. 
 
Ex: TooOldException, TooYoungException, .....
 
 
 

 1. ArrayIndexOutOfBoundException:-

  • It is the child class of RuntimeException and it is unchecked. 
  • Raised automatically by JVM whenever we are trying to access array element with out of range index. 
Ex:- 
 
int[] x = new int[4];    //   0 to 3
 System.out.println( x[0]   );  
 
System.out.println( x[10]   );  // RE: ArrayIndexOutOfBoundException
 System.out.println( x[22]   ); // RE: ArrayIndexOutOfBoundException

2. NullPointerException:-

  • It is the child class of RuntimeException and it is unchecked. 
  • Raised automatically by JVM whenever we are trying to perform any operation on null.
Ex:-
 String s = null; 
 System.out.println( s.length() );  // RE: NullPointerException

3. ClassCastException:-

  • It is the child class of RuntimeException and it is unchecked. 
  • Raised automatically by JVM whenever we are trying to type cast parent object to child object.
Ex:
Object  o  = new Object();
String s = (String) o ;  // RE: ClassCastException

String s = new String("Raka");
Object o = (Object) s;       // No error

Object o = new String();
String s = (String) o ;       // No error

4. StackOverFlowError:-

  • It is the child class of Error and it is unchecked. 
  • Raised automatically by JVM whenever we are trying to perform recursive method call.
StackOverFlow Exception in java

5. NoClassDefFoundError:-

  • It is the child class of Error and it is unchecked. 
  • Raised automatically by JVM whenever JVM unable to find required .class file.
Ex:    java Test    ( if Test.class file is not available then we will get RE: NoClassDefFoundError: Test)

6. ExceptionInInitializerError:-

  • It is the child class of Error and it is unchecked. 
  • Raised automatically by JVM if any exception occurs while executing static variable assignment and static blocks
Ex:
class Test
{
     static int a = 10/0;
}
 
RE: Exception in thread "main" java.lang.ExceptionInInitializerError
           Caused by: java.lang.ArithmeticException: / by zero
         at com.test.Test.<clinit>(Test.java:5)


class Test 
{
     static {
      String s = null;
      SOP(   s.toString() );
    }
}
RE: Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at com.test.Test.<clinit>(Test.java:7)

7. IlligalArgumentException:-

  • It is the child class of RunTimeException and it is unchecked. 
  • Raised explicitly either by programmer or by API developer to indicate that a method has been invoked with illegal argument.

Ex: The valid range of Thread priorities is 1 - 10. If we are trying to set the priority with any other value then we will get RuntimeException saying IllegalArgumentException.
 
Thread t = new Thread();
t.setPriority(7);
t.setPriority(15);  // RE:  IllegalArgumentException
 

8. NumberFormatException:-

  • It is the direct child class of IllegalArgumentException which is child of RunTimeException and hence it is unchecked.
  • Raised explicitly either by programmer or by API developer to indicate that we are trying to convert string to number and the string is not properly formated.
Ex: 
     int i= Integer.parseInt("100");  // No error
     int i= Integer.parseInt("Raka");   // RE: NumberFormatException
 

9. IllegalStateException:-

  • It is the child class of RunTimeException and it is unchecked.
  •  Raised explicitly either by programmer or by API developer to indicate that a method has been invoked at wrong time.
Ex: After starting thread, we are not allowed to restart once again, otherwise we will get run time exception saying IllegalStateException.

Thread t = new Thread();
t.start();

t.start(); // IllegalStateException

10. AssertionError:-

  • It is the child class of Error and it is unchecked.
  •  Raised explicitly either by programmer or by API developer to indicate that assert statement that fails.
Ex:       assert ( x>10 )
 If x is not greater than 10 then we will get RuntimeException: AssertionError

 
Top 10 exceptions in java



RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post