When an unwanted, unexpected event that disturbs the normal flow of our program is called Exception. We have discussed what is exception and how to handling exception in Java in the previous tutorial. Now we are going to discuss the top 10 Exceptions in Java.

Based on the source, who triggers the exception, all exceptions are divided into 2 types.

  1. JVM Exceptions
  2. Programmatic Exceptions

JVM Exceptions

The Exception which is raised automatically by the JVM, whenever a particular event occurs is called JVM Exceptions.

Ex: ArrayIndexOutOfBoundsException

NullPointerException

Programmatic Exceptions

The Exceptions which are raised explicitly either by the programmer or API developer are called programmatic exceptions

Ex: IllegalArgumentException

NumberFormatException

Top 10 Exceptions


ArrayIndexOutOfBoundsException

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised automatically by the JVM whenever we are trying to access the array element with out of range index.

Example

public class Top10Exceptions {
    public static void main(String[] args) {
        int a[] = new int[10];
        System.out.println("a [0] "+a[0]);
        System.out.println("a [11] "+a[11]);
    }
}

 

Output:

a [0] 0
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 11
at exceptions.Top10Exceptions.main(Top10Exceptions.java:17)
Java Result: 1

NullPointerException

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised automatically by the JVM, whenever we are trying to perform any operation on null.

public class Top10Exceptions {
    public static void main(String[] args) {
        String s = null;
        System.out.println("s length : "+s.length());
    }  
}

Output:

Exception in thread “main” java.lang.NullPointerException
at exceptions.Top10Exceptions.main(Top10Exceptions.java:16)
Java Result: 1

StackOverflowError

It is the child class of VirtualMachineError, hence it is unchecked and it is raised by the JVM. it will raise whenever we are trying to perform recursive method invocation.

Ex:

public class Top10Exceptions {
    public static void main(String[] args) {
        m1();
    }  
    public static void m1(){
        m2();
    }
    public static void m2(){
        m1();
    }
}

Output:

Exception in thread “main” java.lang.StackOverflowError
at exceptions.Top10Exceptions.m2(Top10Exceptions.java:21)

NoClassDefFoundError

It is the child class of LinkageError , hence it is unchecked and raised automatically by the JVM. Whenever the JVM unable to locate the required classes in the classpath. Then it will raise this NoClassDefFoundError.

Ex: Java SampleClass

on the above example, I am trying to run the class SampleClass.class. if the Sample.class is not available then the JVM raise this error.

ClassCastException

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised automatically by the JVM, whenever we are trying to typecast parent object to child type.

Ex:

public class Top10Exceptions {

    public static void main(String[] args) {
        String s = new String("Online tutorials point");
        Object o = (Object) s;
        System.out.println("o : " + o);

        Object o1 = new Object();
        String s1 = (String) o1;
        System.out.println("s1 : " + s1);

    }
}

Output:

o : Online tutorials point
Exception in thread “main” java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
at exceptions.Top10Exceptions.main(Top10Exceptions.java:20)
Java Result: 1

ExceptionInInitializerError

It is the child class of LinkageError , hence it is unchecked and raised automatically by the JVM. If any exception raised while performing initialization for static variables and while executing static blocks, the JVM throws this error.

public class Top10Exceptions {

    static int i = 100 / 0;

    public static void main(String[] args) {
        System.out.println("i : " + i);
    }
}

Output:

Exception in thread “main” java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at exceptions.Top10Exceptions.<clinit>(Top10Exceptions.java:13)

IllegalArgumentException

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised automatically by the JVM. Raised explicitly by the programmer or by the API developer to indicate that a method has been invoked with an invalid argument.

Ex:

public class Top10Exceptions {

    public static void main(String[] args) {
        Thread t = new Thread();
        t.setPriority(1000);
    }
}

Output:

Exception in thread “main” java.lang.IllegalArgumentException
at java.lang.Thread.setPriority(Thread.java:1086)
at exceptions.Top10Exceptions.main(Top10Exceptions.java:16)

NumberFormatException

It is the child class of IllegalArgumentException and hence it is an unchecked Exception. It is raised automatically by the JVM. Raised explicitly by the programmer or API developer to indicate that trying to convert String to number type or the String is not properly formatted.

Ex:

public class Top10Exceptions {

    public static void main(String[] args) {
        int i = Integer.parseInt("one");
    }
}

Output:

Exception in thread “main” java.lang.NumberFormatException: For input string: “one”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

 IllegalStateException

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised by the programmer or API developer to indicate that a method has been invoked at an inappropriate time.

public class Top10Exceptions {

    public static void main(String[] args) {
        Thread t = new Thread();
        t.start();
        t.start();
    }
}

We can’t start a thread twice.

Output:

Exception in thread “main” java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at exceptions.Top10Exceptions.main(Top10Exceptions.java:17)

AssertionError

It is the child class of RuntimeException and hence it is an unchecked Exception. It is raised by the programmer or API developer to indicate that the assert statement is failed.

Ex: assert(false);

Happy Learning 🙂