< Prev
Next >

Unchecked Exceptions

What are Unchecked Exceptions?

"Unchecked Exceptions" are named so because they are left unchecked by the Java Compiler at the compile time of a program. Hence, by not checking for such exceptions at the compile time, the Java compiler does not ask us to handle the unchecked exceptions.

Some important points about unchecked exceptions

Example of Unchecked Exception - ArithmeticException

// An example of unchecked exception

class UncheckedExceptionEx
public static void main(String... ar)


Exception in thread "main" java.lang.ArithmeticException: / by zero
        at UncheckedException.main(

Here in this code above, an integer(100) is divided by a zero value. Division of an integer by zero is an illegal operation in Java and it leads to an unchecked exception of type ArithmeticException.


Types of Unchecked Exceptions

Unchecked Exceptions Description
ArrayIndexOutOfBoundsException This Exception is thrown when we try to access an array with an invalid index value. For example-
int arr[3]={1,2,3};

The last statement will throw an ArrayIndexOutOfBoundException, as we are trying to access array with an invalid index value i.e. 4, which is an invalid index for an array with the size 3.
NullPointerException This Exception is thrown when ab object is initialized with a null value and we try to call a method on it. For example-
String str=null;
ArrayStoreException This Exception is thrown when a wrong type of object is made to store in an array of objects. For example -
Object array[]= new Integer[3];
array[0]= new String("Hello");

Here, an array of Object class was initialized to hold 3 objects of type Integer. But an attempt was made to store a String object.
ClassCastException This Exception is thrown when an attempt is made to cast a reference variable to a type of which it is not an instance. For example- if A is a superclass of B and if we try to perform
A ob= new A();
B ob2= (B)ob; //casting

This casting of a superclass reference variable to a subclass type will lead to a ClassCastException.
NumberFormatException This Exception is thrown when a method that converts the String to a number, received a String that cannot be parsed to a number. For example-
String str="hello";
int i= Integer.parse(str);

Here, parse() method tries to convert an String made up of characters to an number(which is illegal) and it leads to NumberFormatException.

Please share this article -

< Prev
Next >
< Checked Exception
Exception Propagation >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition