Exception Handling in Java

Shiv Kumar
3 min readMay 17, 2021

Exception Handling is a very powerful feature of Java using which we can handle exceptions. In Java, an Exception is an Object that describes an unexpected condition. This object is thrown whenever there is an unexpected condition is present. The normal flow of the program stops and can only be resumed when the Exception is handled. We can handle these Exceptions within our code.

WHAT IS EXCEPTION:- An Exception is an abnormal or unexpected condition that can occur during the execution of the program.

HIERARCHY OF EXCEPTION:-

The Throwable class is at the top of the Exception hierarchy. There are two subclasses of the Throwable class named Exceptions and Error.

ERROR:- Error are those exceptions that your program cannot handle, i.e. you cannot handle the exceptions under the Error class. The Error usually arises on the level of JVM. In this blog, we will not discuss Errors anymore.

EXCEPTIONS:- The Exceptions class indicates that exception, which can be caught and handled by your program. We will discuss these exceptions and their handling in detail.

TYPES OF EXCEPTIONS:- The Exceptions can be categorized into two main categories, Checked and Unchecked Exceptions.

1. Checked Exceptions:- Checked Exceptions are also known as Compile-time Exceptions. These Exceptions can be caught by the compiler itself. Examples are ClassNotFoundException, IOException, etc.

2. Unchecked Exceptions:- Unchecked Exceptions are known as Run-time Exceptions. These exceptions are caught by JVM. Examples are NullPointerException, ArrayIndexOutOfBoundsException, etc.

EXCEPTION HANDLING:-

This is the general form of an exception-handling block:


try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// …
finally {
// block of code to be executed after try block ends
}

> Try block:- This is the block where your normal code resides. The Exception is caught in this part of the code.

> Catch block:- When an error is thrown by the try block, the catch block is executed. Simply, a catch block is executed when an Exception is thrown. We can define different catch blocks for different types of Exceptions.

> Finally block:- Code in this block is compiled every time, whether an Exception is caught or not. So, we can put important code in this block.

MANUAL EXCEPTION CREATION:- We can also create Exceptions in Java manually.

class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
System.out.println(“Inside throwOne.”);
throw new IllegalAccessException(“demo”);
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
System.out.println(“Caught “ + e);
}
}
}

>Throw keyword:- Throw keyword is used to throw the exception. In the above code, an IllegalAccessException is thrown using the throw keyword.

> Throws keyword:- Throws keyword is used to throw the Exception to a different method. Either, the Exception is not handled in the method of origin, or if we want to propagate the Exception to another method, the Throws keyword can be used. In the above code, throwOne() does not handle the IllegalAccessException and it is being thrown to main() using the Throws keyword.

These five keywords i.e., try, catch, finally, throw, and throws sum up the Exception handling in Java.

--

--