Try Block Raising Multiple Exceptions

Java Exception Multiple Catch Blocks

We know that a try block in Java will have a matching catch block. Now, we see how to use multiple catch blocks. Have a look at the picture.

The picture shows there are three code snippets within the try block. Let us assume code snippet 2 depends on code snippet 1 and code snippet 3 depends on code snippet 2. We will also assume each code snippet has the possibility of one exception type. From the picture, we can assume the exception types are A, B and C.

Separating Error Handling Code

Java Exception Handling: Try Catch Finally

When application crashes the JVM excludes it from any further execution. This means application halts abruptly. In Java Exception Handling is useful to avoid application crashes. Exception handles error condition and takes a safer path in the execution flow. The exception also makes code more readable as the error handling code stays in a separate section from the normal business logic code. In this example, we will see how to handle exception using Try & Catch blocks. We will also see the role of Finally block.