Java -Exception-hierarchy-types-example

An exception is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

The Exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

Exception Hierarchy

All exception and errors types are sub classes of class Throwable.One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. IndexOutOfBoundsException is an example of such an exception.Another branch,Error are used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). OutOfMemoryError is an example of such an error.

We have three categories of Exceptions

Checked exceptions  

A checked exception is an exception that is checked (notified) by the compiler at compilation-time, these are also called as compile time exceptions. These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions.


class Main { 
    public static void main(String[] args) { 
        FileReader file = new FileReader("D:\\test\\knf.txt"); 
        BufferedReader fileInput = new BufferedReader(file); 
        // Print first 9 lines of file "D:\test\knf.txt" 
        for (int counter = 0; counter < 9; counter++)  


Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
        at Main.main(

Unchecked exceptions

An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.


class Main {
public static void main(String[] args) {

int a = 1;
int b = 0;
int c = a / b;


Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(


These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Exceptions Methods

Method & Description

public String getMessage():
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.

public Throwable getCause():
Returns the cause of the exception as represented by a Throwable object.

public String toString():
Returns the name of the class concatenated with the result of getMessage().

public void printStackTrace():
Prints the result of toString() along with the stack trace to System.err, the error output stream.

public StackTraceElement [] getStackTrace():
Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.

public Throwable fillInStackTrace():
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.

This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Exception Handling?click here