Exception Types
In this tutorial, we are going to discuss exception types in Java. All Exceptions are divided into two categories.
JVM Exceptions
Raised automatically by the JVM whenever a particular condition occurs.
E.g
ArithmeticException, NullPointerException.
Programmatic Exceptions
These are raised programmatically because of programmers code or API’s developer’s Code.
E.g
IllegalArgumentException, NumberFormatException.
1. NullPointerException
It is the direct child class of RuntimeException, and it is unchecked. Thrown automatically by the JVM whenever we are performing any operation on null.
String s = null
System.out.println(s.length()); -> NullPointerException
2. StackOverFlowError
It is the child class of Error, and it is unchecked. Raised automatically by the JVM whenever we are performing recursive method invocation.
public class Test {
public static void m1() {
m1();
}
public static void main(String arg[]) {
m1();
}
}
3. ArrayIndexOutOfBoundsException
It is the child class of RuntimeException, and it is unchecked thrown automatically by the JVM whenever we are accessing an array element with an invalid int index. (Out of range index)
int [] a = {10, 20, 30};
System.out.println(a[0]);
System.out.println(a[20]); -> ArrayIndexOutOfBoundsException.
4. ClassCastException
It is the child class of RuntimeException, and it is unchecked. Thrown automatically by the JVM whenever we are trying to typecast parent class object to the child type.
String s = "raju";
Object o = (Object)s // Fine
Object o = new Object();
String s = (String)o; // Error
R.E: ClassCastException.
5. NoClassDefFoundError
It is the child class of Error and it is unchecked. Thrown automatically by the JVM if the required .class file is not available.
E.g
java Ashok
If Ashok.class file is not available we will get NoClassDefFoundError.
6. ExceptionInInitializerError
It is a child class of Error, and it is unchecked. Raised automatically by the JVM whenever an Exception occurs during initialization of static variables or execution of static blocks.
public class Test {
static int i = 10/0;
}
7. IllegalArgumentException
It is the child class of RuntimeException. It is unchecked thrown explicitly by the programmer or API developer whenever we invoke a method with an inappropriate or invalid argument.
E.g
The valid range of Thread priority is 1 – 10. If we are trying to invoke the setpriority method with 100 as an argument, we will get IllegalArgumentException.
public void setPriority(int i) {
if (i>10 || i<11) {
throw new IllegalArgumentException();
}
}
8. NumberFormatException
It is the child class of Illegal Argument, and it is unchecked. Thrown programmatically whenever we attempt to convert String to Number type, but the String is not formatted properly.
String s = 10;
int i = Integer.parseInt(s); // Fine
String s = "ten";
int i = Integer.parseInt(s); -> NumberFormatException
9. IlleaglStateExcepiton
It is the child class of RuntimeException, and it is unchecked. Thrown programmatically to indicate that a method has been invoked at an inappropriate time.
E.g
- After invalidating a session object, we are not allowed to call any method on that object; otherwise we will get IllegalStateException.
- After committing the response, we are not allowed to redirect or forward; otherwise we will get IlleagalStateException
- After starting a thread, we are not allowed to start the same thread once again; otherwise, we will get IlleagalStateException.
MyThread t = new MyThread();
t.start();
t.start(); -> IllegalThreadStateException
10. AssertionError
It is the child class of Error, and it is unchecked thrown programmatically to indicate Assertion fails.
E.g
Assert(false); -> AssertionError
That’s all about the Exception Types in java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!