0% found this document useful (0 votes)
9 views3 pages

Java Multi-Catch Exception Handling

The document discusses using multi-catch blocks in Java to handle different exceptions. A multi-catch block allows performing different tasks for different exceptions in a single try-catch block. It provides an example of a multi-catch block that handles ArithmeticException, ArrayIndexOutOfBoundsException, and general Exception. The key rules are that only one catch block will be executed for a single exception, and catch blocks must be ordered from most specific to most general.

Uploaded by

shivaji boss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Java Multi-Catch Exception Handling

The document discusses using multi-catch blocks in Java to handle different exceptions. A multi-catch block allows performing different tasks for different exceptions in a single try-catch block. It provides an example of a multi-catch block that handles ArithmeticException, ArrayIndexOutOfBoundsException, and general Exception. The key rules are that only one catch block will be executed for a single exception, and catch blocks must be ordered from most specific to most general.

Uploaded by

shivaji boss
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java catch multiple exceptions

Java Multi catch block


If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch
block.

Let's see a simple example of java multi-catch block.

public class TestMultipleCatchBlock{

public static void main(String args[]){

try{

int a[]=new int[5];

a[5]=30/0;

catch(ArithmeticException e){[Link]("task1 is completed");}

catch(ArrayIndexOutOfBoundsException e){[Link]("task 2 completed");}

catch(Exception e){[Link]("common task completed");}

[Link]("rest of the code...");

Test it Now
Output:task1 completed
rest of the code...

Rule: At a time only one Exception is occured and at a time only one catch block is
executed.

Rule: All catch blocks must be ordered from most specific to most general i.e. catch for
ArithmeticException must come before catch for Exception .

class TestMultipleCatchBlock1{

public static void main(String args[]){

try{

int a[]=new int[5];

a[5]=30/0;

catch(Exception e){[Link]("common task completed");}

catch(ArithmeticException e){[Link]("task1 is completed");}

catch(ArrayIndexOutOfBoundsException e){[Link]("task 2 completed");}

[Link]("rest of the code...");

Test it Now

Output:
Compile-time error

prev next

Please Share

Latest 4 Tutorials

PouchDB DB2 MariaDB

[Link]

You might also like