Wednesday , January 18 2017
Home / java / Top 10 Exceptions in Java

Top 10 Exceptions in Java

When unwanted, unexpected event that disturbs the normal flow of our program is called Exception. We have discussed about what is exception and how to handling exception in Java in the previous tutorial. Now we are going to discuss about 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 are raised automatically by the JVM, when ever a particular event occurs are 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 unchecked Exception. It is raised automatically by the JVM, when ever 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 unchecked Exception. It is raised automatically by the JVM,when ever 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 when ever 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. When ever the JVM unable to locate the required classes in the class path. 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 unchecked Exception. It is raised automatically by the JVM, when ever 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 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 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 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 unchecked Exception. It is raised by the programmer or API developer to indicate that a method has been invoked at 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 unchecked Exception. It is raised by the programmer or API developer to indicate that assert statement is failed.

Ex: assert(false);

You may also Like :

What is Exception in Java

Exception Handling in Java

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

One comment

  1. chagantirajesh.cultuzz@gmail.com'
    rajesh chaganti

    Thank u chandrashekhar…..these will help a lot while especially not only for freshers & laterals too…. to the software industry…in my early stage i too faced all kids of errors above…Good post….keep posting….

Leave a Reply

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