Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Exception Bubbling in Java

1. Exception Bubbling

In java and modern OOPS language, an Exception moving from one method to other method is what we call as Exception Bubbling. In java, a method can call other method. The Called method can call one more method. These kinds of calling sequence continue and we can see all the methods in a call stack for a given point in time.

When a method in the call stack raises an exception, the exception will search for a matching catch block in the current function. When it does not find a matching catch block or if there no catch block at all, Java runtime hands over the exception to the calling function. The sequence continues like till the very base function in the call stack. We can see this as an exception is bubbling from the top of the call stack to bottom.

2. Exception Bubbling – How it works?

We can study exception bubbling concept with a picture shown below:

Exception Bubbling in Java
Exception Bubbling in Java

In the above picture, we see three functions: Fx, Fy and Fz. Here, the function Fx is calling function Fy and function Fy calls function Fz. Let us assume the function Fz has the possibility of rising the Exception of Type T. Let us say this exception as ExceptionT.

When the function Fz rises an Exception, JVM searches for the matching catch block in the function Fz. There are three situations arise here. They are:

  1. The function not having the try & catch block at all.
  2. Function is having a catch block. But there is not matching handler to handle exception of type ExceptionT.
  3. Catch block matching the with ExceptionT.

In the third case, the exception gets handled. For the first two cases, the exception traverse back in the call stack in-search of the matching catch block. We call this as Exception Bubbling. In the above picture, you can see how ExceptionT is traversing back in the all stock to find its matching handler. Now let us go to the example.

3. Program Entry

Below is the code for the Main Program Entry:

The code is simple. Here, we are making call to the function funx. After calling the function, JVM exits this example. Now let us look at the funx.

4. The Method funx() with Try Catch

The function funx will make a call to one more function funy. We surround this calling code within try & catch block. In the catch block, we try to handle to the arithmetic exception. One may think ArithmeticException is not relevant here. But we place this catch block for exploring the Exception Bubbling. Before and after calling the function, we have console output statements to know the code execution flow at runtime. Implementing function funx is below:

5. Intermediate Method – funy()

The method funy does nothing but making call to the one more function funz. We have this function to study the Exception Bubbling. Below is the code:

6. The Method funz() – Raising Exception

The method funz is having an invalid division at line number 5. This line will raise ArithmeticException Exception because of division by zero operation. But there no exception handling code here in this function. Have a look at the code below:

Since we do not have an Exception handler here, JVM moves this exception to the caller of this function. In our case, it is funy. But there is no handler there in funy too. JVM again moves back to the caller in the call stack. The caller for funy is funx. The function funx has a matching handler for the ArithmeticException and hence it handles the exception raised deep in the call stack. Means, funz raises the exception and it is handled by the funx because of the matching handler. The movement of exception from funz -> funy -> funx is called Exception Bubbling.

7. Complete Code Example for Exception Bubbling

8. Watch This Example as Video


Exception Bubbling Example

Categories: Java

Tags: ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.