Advertisement
// An example of unchecked exception
class UncheckedExceptionEx
{
public static void main(String... ar)
{
System.out.println(100/0);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at UncheckedException.main(UncheckedException.java:5)
Advertisement
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}; System.out.println(arr[4]); 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; System.out.println(str.length()); |
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. |
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement