- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiple catch in Java
To catch different exceptions generated by a single try block, we use the multiple catch block in Java.
The syntax for a multi-catch block is as follows −
try { } catch(Exception1 | Exception2 | Exception3…..| ExceptionN exception_obj) { }
Let us see a program for multiple catch block in Java −
Example
import java.util.Scanner; public class Example { public static void main(String args[]) { Scanner sc = new Scanner(System.in); try { int n = Integer.parseInt(sc.next()); System.out.println(n/0); } catch (ArithmeticException | NumberFormatException e) { System.out.println("Exception Caught: " + e); } } }
The output depends on the input.
For the following input −
5
Output
For the following input −
Exception Caught: java.lang.ArithmeticException: / by zero
Hello
Output
Exception Caught: java.lang.NumberFormatException: For input string: "Hello"
- Related Articles
- Is it possible to catch multiple Java exceptions in single catch block?
- Can a try block have multiple catch blocks in Java?
- Can we define a try block with multiple catch blocks in Java?
- Exception Hierarchy in case of multiple catch blocks.
- Multi-catch in Java
- Is it possible to have multiple try blocks with only one catch block in java?
- PHP Exception Handling with Multiple catch blocks
- How to catch multiple exceptions in one line (except block) in Python?
- What is the catch block in Java?
- What are unreachable catch blocks in Java?
- Try, catch, throw and throws in Java
- Can we declare a try catch block within another try catch block in Java?
- How to catch an assertion error in Java
- What are try, catch, finally blocks in Java?
- Flow control in a try catch finally in Java

Advertisements