Different types of Exception in Java

Top Exceptions in Java

On the basis of source which triggers the exception, we divide exceptions into 2 types as:

1) JVM Exceptions:

Exceptions which arrise automatically by the JVM whenever a particular event occurs are called JVM exceptions. For example:

a) ArrayIndexOutOfBoundsException: It is the child class of RuntimeException and hence it is uncheck. It arrise automatically by the JVM whenever we are trying to access array elements with out of range index. Like in the following case

int []ar = new int[10];
System.out.println(ar[0]);      //this line will compile successfully
System.out.println(ar[250]);    //here the runtime exception will be thrown by the JVM

c) NullPointerException: It is also the child class of RuntimeException and is a uncheck exception.  JVM throws this exception when we perform any operation on Null. For example,

String str;                         //string defined with no values
System.out.println(str.length());   //here nullpointer exception will be thrown

d) StackOverFlowError: It is the child class of Error and it is uncheck. It arrise by the JVM when the programmer is trying to perform recursive method invocation. For example,

public class ExampleTwo
{
    public static void method1()
    {
        method2();
    }
    public static void method2()
    {
        method1();
    }
    public static void main(String []args)
    {
        method1();
    }
}

e) NoClassDefFoundError: It is a child class of Error and it arrise when requirement of the particular class is not fulfill by the JVM. For example, try the following code in the command prompt

java ExampleThree

f) ClassCastException: It is a child class of Runtime Exception and JVM throws it, when it tries to type cast the parent object into the child type object. For example,

Object obj = new Object();
String str = (String)obj;              //invalid

String str = new String("Hello World");
Object obj = (Object)str;              //valid

g) ExceptionInInitializerError: It is a child class of Error and it arrise when JVM when any exception occurs while performing initialization for static variables and while executing static blocks. For example,

class ExampleThree
{
    static int i = 10/0;                        //here ExceptionInInitializerError will be raised
    static
    {
        String str = null;
        System.out.println(str.length());       //here ExceptionInInitializerError will be raised
    }
}

2. Programmatic Exceptions:

The exceptions which arrises explicitly either by the programmer or by the API developer are the programmatic exceptions. For example:

a) IllegalArgumentException: It is the child class of Runtime Exception and it arrise explicitly by the programmer or by API developer to indicate that a method has been invoked with valid argument. For example,

Thread th = new Thread();
th.setPriority(10);
th.setPriority(250);       //here IllegalArgumentException will be raised

b) NumberFormatException: It is the child class of Runtime Exception. It is thrown to indicate that the programmer is trying to convert string to a number type but the string is not properly formatted. For example,

int i = Integer.parseInt("10");       //successfully compiled
int j = Integer.parseInt("Ten");      //NumeberFormatException will be thrown by this line

c) IllegalStateException: It is the child class of Runtime Exception and it indicates that a method has been inappropriate invocation at inappropriate time. For example,

HttpSession sess = req.getSession();
System.out.println(sess.getId());       //valid

sess.invalidate();                       
System.out.println(sess.getId());       //here the exception will be thrown
//once the session is invalidated, no method can be called through it

d) AssertionError: It is the child class of Error and it arrises to indicate that assert statement fails. For example,

assert(false);

This is all about the exceptions in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *