Table of Contents
In this tutorial, you’ll learn about Exception in JAVA, Types of Exception, Java Exception Hierarchy and more.
What is Exception ?
An exception is an abnormal condition that arises in a code sequence .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.
Types of Exception
1. Compile Time Exception — Checked Exception
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, you will get compilation error.
- SQLException
- IOException
- ClassNotFoundException
2. Run Time Exception — Unchecked Exception
These exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit.
- ArithmeticException
- NulPointerException
- ArrayIndexOutOfBoundsException
Java Exception Hierarchy
Every Exception in Java is the subtype of Exception class which in turn is the subclass of Throwable. And as we know everything in Java derived from Object class Throwable also derived from class Object. Exception and Error are two different classes that derived from Throwable. Errors represent a situation that doesn’t occur because of a Programming error but that might happen at the time of program execution and these are abnormal behavior which java program cannot handle and shouldn’t bother to handle.