Exception-hierarchy
Hierarchy of Java Exception Classes
- The java.lang.throwable class is the root class of the Java exception hierarchy inherited by two sub-classes: Exception and 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
- By using a try-catch block
- By using the throws declaration keyword
Key Points:
- 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
- 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.