There are some common unchecked exceptions occur in day to day java programming. Here we are going to see scenarios when those unchecked exceptions may occur.
1) NullPointerException
When access methods or variables on a null reference then throws NullPointerException
1 2 3 |
Employee emp = null; emp.name = "sekhar"; // Accessing variable on null reference causes NullPointerException emp.getName(); // Accessing method on null reference causes NullPointerException |
2) ArithmeticException
When an integer “divide by zero” throws ArithmeticException.
1 |
int result = 1/0; // Arithmetic Exception |
3) NumberFormatException
When we try to convert string into a numeric value such as float or integer or long but the format of the input string is not appropriate or illegal.
1 2 |
int x = Integer.parseInt(null); // Throws NumberFormatException int y = Integer.parseInt("100A"); // Throws NumberFormatException |
4) ArrayIndexOutOfBoundsException
When access illegal index of an array
1 2 3 4 |
int[] values = new int[] {1, 2, 3}; int valueFromNegativeIndex = values[-1]; // access at negative index int valueFromGreaterIndex = values[4]; // access at greater index int valueFromLengthIndex = values[3]; // access at index equal to size of the array |
5) StringIndexOutOfBoundsException
When access illegal index of String with it’s methods
1 2 3 4 5 6 |
String name = "SomaSekhar Reddy"; char charAtNegativeIndex = name.charAt(-1); // access at negative index char charAtLengthIndex = name.charAt(18); // access at index equal to size of the string int intAtNegativeIndex = name.codePointBefore(-1); // access at negative index int intAtLengthIndex = name.codePointBefore(18); // access at index equal to size of the string |
6) IllegalArgumentException
When a method has been called by passing an illegal or inappropriate argument
1 2 3 |
Thread thread = new Thread(); thread.sleep(-1); thread.setPriority(20); |
7) IllegalStateException
When a method has been invoked either at illegal or inappropriate time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.vidvaan.corejava.exception18.common.unchecked.exceptions; import java.util.ArrayList; import java.util.Iterator; import java.util.List; //common unchecked exception example : IllegalStateException public class IllegalStateExceptionDemo { public static void main(String args[]) { List<String> arrayList = new ArrayList<String>(); arrayList.add("a"); arrayList.add("b"); Iterator<String> it = arrayList.iterator(); while (it.hasNext()) { it.remove();// calling remove() without calling next() throws IllegalStateException } } } |
8) ClassCastException
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.vidvaan.corejava.exception18.common.unchecked.exceptions; //common unchecked exception example : ClassCastException class A { } class B extends A { } class C extends B { } public class ClassCastExceptionDemo { public static void main(String[] args) { A a = new B(); // B type is auto up casted to A type C c = (C) a; // Here, you will get class cast exception } } |