0% found this document useful (0 votes)
15 views2 pages

3 Java Multiple Catch Block Example

The document explains how to use multiple catch blocks in Java to handle different exceptions. It provides examples demonstrating the correct order of catch blocks, emphasizing that they should be arranged from most specific to most general. Additionally, it highlights that only one exception can occur at a time, and only one catch block will be executed.

Uploaded by

Anima
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)
15 views2 pages

3 Java Multiple Catch Block Example

The document explains how to use multiple catch blocks in Java to handle different exceptions. It provides examples demonstrating the correct order of catch blocks, emphasizing that they should be arranged from most specific to most general. Additionally, it highlights that only one exception can occur at a time, and only one catch block will be executed.

Uploaded by

Anima
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
You are on page 1/ 2

9/14/2015 Java Multiple catch block example - javatpoint

Content Menu ▼

Java catch multiple exceptions Best


Coding
Java Multi catch block Program
Want to give
If you have to perform different tasks at the occurrence of different
your career a
Exceptions, use java multi catch block.
boost? Join
Let's see a simple example of java multi-catch block.
the
StackRoute
program.
. public class TestMultipleCatchBlock{
. public static void main(String args[]){
. try{
. int a[]=new int[5];
. a[5]=30/0;
. }
. catch(ArithmeticException e)
{System.out.println("task1 is completed");}
. catch(ArrayIndexOutOfBoundsException e)
{System.out.println("task 2 completed");}
. catch(Exception e)
{System.out.println("common task completed");}
.
. System.out.println("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

http://www.javatpoint.com/multiple-catch-block-in-java 1/2
9/14/2015 Java Multiple catch block example - javatpoint

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)
{System.out.println("common task completed");}
. catch(ArithmeticException e)
{System.out.println("task1 is completed");}
. catch(ArrayIndexOutOfBoundsException e)
{System.out.println("task 2 completed");}
. System.out.println("rest of the code...");
. }
. }

Test it Now

Output:

Compile-time error

← prev next →

http://www.javatpoint.com/multiple-catch-block-in-java 2/2

You might also like