Multicatch in Java

Background

Prior to Java 7, we had to catch only one exception type in each catch block. So whenever we needed to handle more than one specific exception, but take same action for all exceptions, then we had to have more than one catch block containing the same code.

In the following code, we have to handle two different exceptions but take same action for both. So we needed to have two different catch blocks as of Java 6.0.

// A Java program to demonstrate that we needed
// multiple catch blocks for multiple exceptions
// prior to Java 7
import java.util.Scanner;
public class Test
{
    public static void main(String args[])
    {
        Scanner scn = new Scanner(System.in);
        try
        {
            int n = Integer.parseInt(scn.nextLine());
            if (99%n == 0)
                System.out.println(n + " is a factor of 99");
        }
        catch (ArithmeticException ex)
        {
            System.out.println("Arithmetic " + ex);
        }
        catch (NumberFormatException ex)
        {
            System.out.println("Number Format Exception " + ex);
        }
    }
}

Input 1:

GeeksforGeeks

Output 2:

Exception encountered java.lang.NumberFormatException: 
For input string: "GeeksforGeeks"

Input 2:

0

Output 2:

Arithmetic Exception encountered java.lang.ArithmeticException: / by zero

Multicatch

Starting from Java 7.0, it is possible for a single catch block to catch multiple exceptions by separating each with | (pipe symbol) in catch block.

// A Java program to demonstrate multicatch 
// feature
import java.util.Scanner;
public class Test
{
    public static void main(String args[])
    {
        Scanner scn = new Scanner(System.in);
        try
        {
            int n = Integer.parseInt(scn.nextLine());
            if (99%n == 0)
                System.out.println(n + " is a factor of 99");
        }
        catch (NumberFormatException | ArithmeticException ex)
        {
            System.out.println("Exception encountered " + ex);
        }
    }
}

Input 1:

GeeksforGeeks

Output 1:

Exception encountered java.lang.NumberFormatException:
For input string: "GeeksforGeeks"

Input 2:

0

Output 2:

Exception encountered 
java.lang.ArithmeticException: / by zero

A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler, that is, the bytecode has no replication of exception handlers.

Important Points:

  • If all the exceptions belong to the same class hierarchy, we should catching the base exception type. However, to catch each exception, it needs to be done separately in their own catch blocks.
  • Single catch block can handle more than one type of exception. However, the base (or ancestor) class and subclass (or descendant) exceptions can not be caught in one statement. For Example
    // Not Valid as Exception is an ancestor of 
    // NumberFormatException
    catch(NumberFormatException | Exception ex) 
    
  • All the exceptions must be separated by vertical bar pipe |.

This article is contributed by Aparna Vadlamani. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.