Exception Handling in Java.

Prithvi Dev
Javarevisited
Published in
6 min readSep 3, 2021

Hello Guys, This article will include most about Exception handling in Java. Most of us, who try to learn and get good at programming languages like java, python, c#, and others, struggles to find good, simple and useful content on the internet.

Most of our time goes in finding the good websites and YouTube videos or tutorials to learn coding.

In this article, I will write about exception handling in java, which will help you to get the good knowledge of the topic. Will also add interview questions in the end of this article.

Firstly,

What is an Exception?

An exception is like an obstacle that disturbs the normal flow of the program during the execution of the program. Due to this, the program will stop or discontinue and this is really not recommended.

Definition: An exception is an unexpected event, that usually arises during the execution of the program. This leads to the disruption to the normal flow of the program and then the program get stops abnormally.

What is Exception Handling?

An Exception Handling is a technique which helps to retain the normal flow of the program, even if the unexpected problems arise in the code during the execution. Commonly, The problems arise are:

  1. Wrong input data formats,
  2. Network Connection Failure,
  3. Opening a non-existing file and more.
Steps followed to handle an Exception

Exception Hierarchy

Exception Hierarchy

Types of Exceptions

Types of Exception

There are two types of exceptions in java:

  1. Checked Exceptions
  2. Unchecked Exceptions
  3. Error

Checked Exceptions: An exception that is handled or checked by the compiler at compilation time only. Example are IOException, SQLException, Interrupted Exception, FileNotFoundException, etc.

Unchecked Exceptions: An exception that is handled or checked at the time of execution or runtime only. Example are ArithmeticException, NumberFormat Exception, ArrayIndexOutOfBoundsException, SecurityException, etc.

Error: Errors are irreversible. It always happens at runtime. It is caused by the environment in which the application is running. Example are OutOfMemoryError, AssertionError, etc.

Basic Exception Handling Program

Built-in Exceptions

These are the exceptions which are already present in the Java Libraries. These can be helpful to handle certain error situations.

List of some Built-in exceptions are:

ArithmeticException

It is thrown when an exception occurred due to some arithmetic operations.

Output for the above code

ArrayIndexOutOfBoundException

Output for the above code

NumberFormatException

Output for the above code

StringIndexOutOfBoundsException

Output for the above code

and many more…

User Defined Exceptions

User Defined Exceptions are the exceptions which are not present in the java exception library. We programmers used to create and use our own designed exceptions in order to enhance the error statements for better understanding.

Java provides us the facility to develop our own exceptions and throw exception using throw keywords.

Below I have implemented the user defined exceptions, have a look

Output for the above code

Exception Handling Keywords

Java Exception handling is mainly managed by five keywords:

Keywords used in Java Exception Handling

Now I will elaborate the keywords below.

Try

This block is used to specify a block where we should place the exception code.

Syntax:

try{//Code that throws an exception}

Nested Try: one or more try blocks inside a try block.

Catch

This block is used to handle the exception.

Syntax:

catch(Exception e){// handling of exception}

Multi Catch: One or more catch blocks in one program.

Finally

This Block is used to specify the important code, which we want to run anyway. Even if an exception arise and not handled using catch block, finally block statements will run, certainly. This block usually include code for closing connections or streams, etc.

Throw

This is a keyword used to throw an exception.

Throws

This is a keyword used to declare exceptions. It does not throw any exception, but tells that there can be any exception in a method. Always used with the method signature. It gives an information to the programmers that there can be exception.

Exception Propagation

Exception propagation is a concept of transferring the exception from one method to its previous method in order to handle the exception.

Let’s see an example and understand better,

In this example, we have taken two methods, m1() and m2(). We have called m2() method in m1() and have raised an exception in m2() method. But we have just raised and exception, but not handled it. So it propagates the exception to the previous method or caller method and now the caller method that is m1() method is responsible for the handling of the exception.

Re throwing Exception

When an exception is raised, firstly the exception is caught by the catch block and then the exception is re thrown using the throw keyword.

See the example for better understanding.

In this, we can clearly see that there will be error in the print statement. The exception raise will be ArithmeticException, and we have handled that using catch block. But we have re thrown the exception and converted the ArithmeticException into other Exception that is NullPointerException.

Converting one exception into other exception type is done with the help of re throwing an exception.

So Guys, I hope you are feeling good and relaxed now as you have done with the exception handling in java. It is the very important concept in java. Most of the interviewers asks from this topic only. But don’t take tension of that too as you can speak about the exception handling confidently.

I am writing some of the differences which will help you a lot for the interview preparation.

Error vs Exception

Throw vs Throws

Some of the interview questions i have been asked are:

  1. Define Exception.
  2. Define Exceptional Handling.
  3. What is throws and throw.
  4. Difference between throw and throws.
  5. What are unchecked and checked exceptions.
  6. What is finally block.
  7. Define built in exceptions and also mention some built in exceptions.
  8. Define user defined exceptions and also mention how you will perform it.
  9. What is Exception Propagation.
  10. What is re throwing an exception.
  11. Base class of exception class.
  12. Tell me the hierarchy of the exception class.

All the best to all of you. I hope you all understood the exception topic in java. It just requires a lot of practice in the particular topic. Try to write code about exception handling. Try to learn and keep in mind about the exceptions and errors arise. This will surely help you in the future.

Thank you all of you

--

--

Prithvi Dev
Javarevisited

Software Engineer specializes in Middleware Technologies and Cloud Platform Administration. Seeking knowledge and practice on DevOps Projects.