Exception-hierarchy
The java.lang.throwable class is the root class of the Java exception hierarchy. It is further divided into two subclasses.
1. Exception2. Error
Hierarchy of Java Exception Classes

Types of Java Exception
There are mainly two types of exceptions
1. Checked Exception
The exception which is identified by the compiler during the compilation time is known as a checked exception- Since the checked exception are known at compilation time it is known as a caught exception.
- The checked exception occurs we must define the handler otherwise the compiler throws an unhandled exception error.
- The checked exception can be handled in two ways
2. By using the throws declaration keyword
Key Point
- The exception is a direct subclass of exception classes except the runtime exception class comes under the checked exception. e.g.. Exception, SQLException, ClassNotFoundExcegption.
- Checked exceptions are known at compile time and occur at runtime.
Example:
package quipoin;
import java.io.File;
import java.io.IOException;
public class CheckedExceptionExmpl {
public static void main(String[] args) {
System.out.println("Program started");
File f1 = new File("c://keshava//JECM84//sample.txt");
try {
f1.createNewFile();
// without try catch block compiler throws exception in this line
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Program started
java.io.IOException: The system cannot find the path specified
at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.base/java.io.File.createNewFile(File.java:1043)
at CheckedExceptionExmpl.main(CheckedExceptionExmpl.java:9)
2. Unchecked exception
The unchecked exceptions are not identified by the compiler and occur at runtime so, it's called as unchecked exception- Since these exceptions are not identified by the compiler it is known as an uncaught exception. It is also called a runtime exception.
- All the exception classes which are a subclass of runtime exceptions come under the runtime exception category Ex. ArithmeticException, NullPointerException.
- Unchecked exceptions are known at execution time and occur at runtime.
Example:
package quipoin;
class UncheckedException {
static void calc(int n1, int n2) {
int res;
res = n1 / n2;
System.out.println(res);
}
public static void main(String[] args) {
calc(15, 5);
}
}
Output:
3
What are Error classes ?
In Java, an error is a subclass of Throwable that tells something serious problem exists, and a reasonable Java application should not try to catch try error.
- Generally, it has been noticed that most of the occurring errors are abnormal conditions and cannot be resolved by normal conditions.
- Error and its subclass are referred to as unchecked exceptions.
- Examples: AssertionError, AbstractMethodError, IllegalAccessError.
Example:
package quipoin;
class StackOverflow {
public static void check(int i) {
if (i == 0) {
return;
} else {
check(i++);
}
}
public static void main(String[] args) {
StackOverflow.check(5);
}
}
Output:
Exception in thread "main" java.lang.StackOverflowError
at com.StackOverflow.check(StackOverflow.java:6)
at com.StackOverflow.check(StackOverflow.java:6)
at com.StackOverflow.check(StackOverflow.java:6)
at com.StackOverflow.check(StackOverflow.java:6)
at com.StackOverflow.check(StackOverflow.java:6)
at com.StackOverflow.check(StackOverflow.java:6)
In the above example, a StackOverflow error means stack memory has fully occupied its space.
Feature | Checked Exception | Unchecked Exception | Error |
---|---|---|---|
Detected at | Compile time | Runtime | Runtime |
Handling | Must be handled | Optional | Cannot be handled |
Examples | IOException, SQLException
| NullPointerException, ArithmeticException
| OutOfMemoryError, StackOverflowError |
Caused By | User / Program Errors | Logical Errors | JVM / Hardware Issues |