9/1/2021 Java Multiple catch block example - javatpoint
Home Java Programs OOPs String Exception Multithreading
OOPs Concepts in Java
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 1/10
9/1/2021 Java Multiple catch block example - javatpoint
Java Catch Multiple Exceptions
Java Multi-catch block
A try block can be followed by one or more catch blocks. Each catch block must contain a different
exception handler. So, if you have to perform different tasks at the occurrence of different exceptions,
use java multi-catch block.
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.
Flowchart of Multi-catch Block
Example 1
Let's see a simple example of java multi-catch block.
OOPs Concepts in Java
MultipleCatchBlock1.java
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:
OOPs Concepts in Java
Arithmetic Exception occurs
rest of the code
⇧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:
ArrayIndexOutOfBounds Exception occurs
rest of the code
OOPs Concepts in Java
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:
Arithmetic Exception occurs
rest of the code
Example 4
In this example, we generate NullPointerException, but OOPs
didn'tConcepts
provideinthe
Javacorresponding exception
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:
OOPs Concepts in Java
Parent Exception occurs
rest of the code
⇧ 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
Help Others, Please Share
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 7/10
9/1/2021 Java Multiple catch block example - javatpoint
Learn Latest Tutorials
digital elasticsearch entity Firewall tutorial
marketing tutorial tutorial framework
Firewall
tutorial
Digital Marketing Elasticsearch
Entity Framework
Functional Google Colab Graph Theory Groovy tutorial
Programming tutorial tutorial
Groovy
tutorial
Google Colab Graph Theory
Functional
Programming
Group Informatica Ionic tutorial ITIL tutorial
Discussion tutorial
Ionic ITIL
tutorial
Informatica
Group Discussion
IOS angular deep learning
Development material tutorial tutorial
tutorial
Angular Material Deep Learning
IOS with Swift
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
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
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence
Hadoop tutorial ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
B.Tech / MCA
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System tutorial
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial OOPs Concepts in Java
tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
⇧ SCROLL Java
TO TOP tutorial Python
Programs
https://www.javatpoint.com/multiple-catch-block-in-java 9/10
9/1/2021 Java Multiple catch block example - javatpoint
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
OOPs Concepts in Java
⇧ SCROLL TO TOP
https://www.javatpoint.com/multiple-catch-block-in-java 10/10