- 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
Is it possible to catch multiple Java exceptions in single catch block?
An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.
Multiple exceptions in a code
Before Java 7 whenever we have a code that may generate more than one exception and if you need to handle the specifically you should use multiple catch blocks on a single try.
Example
The following Java program contains an array of numbers (which is displayed). from user it accepts two positions from this array and, divides the number in first position with the number in second position.
While entering values −
If you choose a position which is not in the displayed array an ArrayIndexOutOfBoundsException is thrown
- If you choose 0 as denominator and ArithmeticException is thrown.
In this program we have handled all the possible exceptions using two different catch blocks.
import java.util.Arrays; import java.util.Scanner; public class MultipleCatchBlocks { public static void main(String [] args) { Scanner sc = new Scanner(System.in); int[] arr = {10, 20, 30, 2, 0, 8}; System.out.println("Enter 3 integer values one by one: "); System.out.println("Array: "+Arrays.toString(arr)); System.out.println("Choose numerator and denominator (not 0) from this array (enter positions 0 to 5)"); int a = sc.nextInt(); int b = sc.nextInt(); try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Warning: You have chosen a position which is not in the array"); } catch(ArithmeticException e) { System.out.println("Warning: You cannot divide a number with 0"); } } }
Output1
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 2 8 Warning: You have chosen a position which is not in the array
Output2
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator (not 0) from this array (enter positions 0 to 5) 1 4 Warning: You cannot divide a number with 0
Multicatch block
From Java 7 onwards a Multicatch block is introduced using this, you can handle more than one exception within a single catch block.
In this, you need to specify all the exception classes to be handled separated by “ | ” as shown below −
catch(ArrayIndexOutOfBoundsException | ArithmeticException exp) { System.out.println("Warning: Enter inputs as per instructions "); }
Example
Following Java program demonstrates the usage of Multicatch. Here we are handling all the exceptions in a single catch block.
import java.util.Arrays; import java.util.Scanner; public class MultiCatch { public static void main(String [] args) { Scanner sc = new Scanner(System.in); int[] arr = {10, 20, 30, 2, 0, 8}; System.out.println("Enter 3 integer values one by one: "); System.out.println("Array: "+Arrays.toString(arr)); System.out.println("Choose numerator and denominator(not 0) from this array, (enter positions 0 to 5)"); int a = sc.nextInt(); int b = sc.nextInt(); try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArrayIndexOutOfBoundsException | ArithmeticException exp) { System.out.println("Warning: Enter inputs as per instructions "); } } }
Output1
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array, (enter positions 0 to 5) 0 9 Warning: Enter inputs as per instructions
Output2
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array, (enter positions 0 to 5) 2 4 Warning: Enter inputs as per instructions
- Related Articles
- Is it possible to have multiple try blocks with only one catch block in java?
- How to catch multiple exceptions in one line (except block) in Python?
- Can a try block have multiple catch blocks in Java?
- What is the catch block in Java?
- Multiple catch in Java
- Can we declare a try catch block within another try catch block in Java?
- How to catch exceptions in JavaScript?
- Can we define a try block with multiple catch blocks in Java?
- Is it necessary that a try block should be followed by a catch block in Java?
- Can we to override a catch block in java?
- How to catch all JavaScript unhandled exceptions?
- How to catch all the exceptions in C++?
- Can we have an empty catch block in Java?
- Can finally block be used without catch in Java?
- Can we have a try block without a catch block in Java?\n
