7 B Java Multiple Catch Block Example - Javatpoint
7 B Java Multiple Catch Block Example - Javatpoint
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 1/10
9/1/2021 Java Multiple catch block example - javatpoint
Points to remember
At a time only one exception occurs and at a time only one catch block is executed.
All catch blocks must be ordered from most specific to most general, i.e. catch for
ArithmeticException must come before catch for Exception.
Example 1
public class MultipleCatchBlock1 {
public static void main(String[] args) {
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 2/10
9/1/2021 Java Multiple catch block example - javatpoint
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Test it Now
Output:
⇧Example 2 TOP
SCROLL TO
https://www.javatpoint.com/multiple-catch-block-in-java 3/10
9/1/2021 Java Multiple catch block example - javatpoint
MultipleCatchBlock2.java
public class MultipleCatchBlock2 {
public static void main(String[] args) {
try{
int a[]=new int[5];
System.out.println(a[10]);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Test it Now
Output:
In this example, try block contains two exceptions. But at a time only one exception occurs and its
corresponding catch block is executed.
MultipleCatchBlock3.java
⇧ SCROLL TO TOP
public class MultipleCatchBlock3 {
https://www.javatpoint.com/multiple-catch-block-in-java 4/10
9/1/2021 Java Multiple catch block example - javatpoint
public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0;
System.out.println(a[10]);
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Test it Now
Output:
Example 4
type. In such case, the catch block containing the parent exception class Exception will invoked.
MultipleCatchBlock4.java
public class MultipleCatchBlock4 {
⇧ SCROLL
TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 5/10
9/1/2021 Java Multiple catch block example - javatpoint
public static void main(String[] args) {
try{
String s=null;
System.out.println(s.length());
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Test it Now
Output:
⇧ SCROLL TO TOP
Example 5
https://www.javatpoint.com/multiple-catch-block-in-java 6/10
9/1/2021 Java Multiple catch block example - javatpoint
Let's see an example, to handle the exception without maintaining the order of exceptions (i.e. from
most specific to most general).
MultipleCatchBlock5.java
class MultipleCatchBlock5{
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 →
Youtube
For Videos Join Our Youtube Channel: Join Now
Feedback
OOPs Concepts in Java
Send your Feedback to feedback@javatpoint.com
https://www.javatpoint.com/multiple-catch-block-in-java 7/10
9/1/2021 Java Multiple catch block example - javatpoint
Preparation
Company
Interview
Questions
OOPs Concepts in Java
Company Questions
Trending Technologies
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 8/10
9/1/2021 Java Multiple catch block example - javatpoint
B.Tech / MCA
https://www.javatpoint.com/multiple-catch-block-in-java 9/10
9/1/2021 Java Multiple catch block example - javatpoint
.Net
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 10/10