What is an Exception in Java? Exception Hierarchy in Java

what is exception? exception hierarchy only fullstack

What is an Exception in Java?

An exception in java is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

The term exception in java is shorthand for the phrase “exceptional event.” Exception is also called as Abnormal condition.

Whenever exception occurs in java then JVM will wrap the cause of that exception or failure into an Exception object and throws it to its parent method. If you dont know about it then thats fine, dont worry. we will look into into it in detail in our next blog.


Need of Exception Handling in Java?

Lets consider an online scenario to understand the need of exception handling in java.

Suppose theres a user who wants to signup to a website. Lets see the steps involved in it.

Why we need Exception Handling in Java?
Why we need Exception Handling in Java?

Step 1- User enters the details and clicks on Submit button.

Steps 2- Website / Server receives the request and calls database to store those details.

Step 3- Ideally the database should store the details into database but lets consider at that time the database server is down…. boom……

Well thats not a normal right? This is an Abnormal Condition where Java language dont have clue what to do here. Thats where Exception Handling comes into picture and handles these abnormal conditions. Lets see how can we handle this exception in this scenario to give nice user experience.

Step 4- Java databse connection code will sends an Exception to saying that the database is down and we cant go ahead.

Step 5- Java code will handle that exception and navigate that user to a Sorry page. Thats how we did exception handling in java and improved user experience.


Exception Hierarchy in Java

Below image shows an Exception Hierarchy in Java. Here all Exception and Errors are Subclass of Throwable class. Here Exception is a class which is used to throw abnormal condition which developer can catch and take appropriate actions for it. Error class is used by the JVM to throw abnormal conditions associated with Java Runtime Environment(JRE) which is not recoverable like StackOverflowException, OutOfMemoryException.

RuntimeException are also called as UnChecked Exception in Java. Some examples of RuntimeExceptions are NullPointerException, ArrayIndexOutOfBoundException. We will look into it in our up coming blog.

Exception Hierarchy in Java
Exception Hierarchy in Java

Error vs Exception in Java

Lets check the difference in between Error and Exception in java.

Key Points

Error

Exception

Type

Unchecked Exception


Java will not force us to handle these Errors
Checked and unchecked exception

Java might or might not force us to handle these Exceptions.
Recovery

Recovery from Error is not possible

Recovery from Exception is possible

Caused by

Caused by the environment(JRE)

Caused by java program

Runtime / Compile time

Errors occur at runtime and not known to the compiler.

All exceptions occurs at runtime but checked exceptions are known to compiler while unchecked are not.

Examples

java.lang.StackOverflowError, java.lang.OutOfMemoryError

Checked Exceptions : SQLException
IOException
Unchecked Exceptions :
NullPointerException
ArrayIndexOutOfBoundException,,.

Exception Handling in Java