Multi-Catch Exception in Java

In this post we’ll see a new feature Multi-Catch exception added in Java 7. Using Multi-catch statement single catch block can handle more than one type of exception. You don’t need to write multiple catch blocks to catch different exceptions where exception handling code is similar.

Prior to Multi-Catch exception in Java

If your code throws more than one type of exception, prior to introduction of Multi-Catch exception in Java 7, you would have used multiple catch blocks one per exception.

Example code

Let’s say you are reading a file and every line of a file has a single integer. You will read each line of the file as string and then convert that string to int. So you need to handle two exceptions in your code IOException and NumberFormatException.

Notice some of the problems here –

  1. Even though exception handling code is same, you are logging the exception and then throwing it again, you have to have separate catch blocks.
  2. It is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.
  3. You may even be tempted to write one catch block with Exception as type to catch all the exception rather than writing multiple catch blocks to catch specific exception. But that is not a good practice.

Using Multi-Catch exception in Java

Java 7 onward you can use multi-catch exception to catch multiple exceptions in one catch block. That way you can avoid duplication of code and also the tendency to use Exception class as a catch-all clause which will hide the real cause.

Java example code using multi-catch exception

Here the same code as used above is written using multi-catch exception

Notice the changes here –

  1. Now there is only a single catch block which combines both the catch blocks.
  2. In multi-catch statement you need to separate each exception type in the catch clause using the pipe (|) symbol.

Catch parameter used in multi-catch statement is final

In a multi-catch statement, handling more than one exception type, catch parameter is implicitly final. In the code above, the catch parameter ex is final and therefore you cannot assign any value to it within the catch block.

Byte code generated by multi-catch statement is superior

Bytecode generated by multi-catch statement in Java will be smaller (and thus superior) in comparison to multiple catch blocks that handle only one exception type each. Multi-catch creates no duplication in the bytecode generated by the compiler.

Multi-Catch statement with exception of same type

Trying to catch exceptions of the same type in multi-catch statement will result in compile time error.

As example –

This multi-catch results in compile time error as FileNotFoundException is the child class of IOException. You will get the error as – The exception FileNotFoundException is already caught by the alternative IOException.

Referencehttps://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html

That’s all for the topic Multi-Catch Exception in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

One Comment

  1. Pingback: try-with-resources in Java - KnpCode

Leave a Reply

Your email address will not be published. Required fields are marked *

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