A try block can be trailed by at least one catch blocks. Each catch block must contain an alternate exception handler. In this way, on the off chance that you need to perform diverse errands at the event of various exceptions, use java multi-catch block.
At once just a single exception happens and at once just a single catch block is executed.
All catch blocks must be ordered from most explicit to most broad, for example catch for ArithmeticException must precede catch for Exception.
Java Program To Perform Multiple Catch Statement
import java.io.DataInputStream;
import java.io.IOException;
class MultipleCatch
{
public static void main(String args[])
{
DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter 2 numbers : ");
try
{
String a = d.readLine();
String b = d.readLine();
int num1 = Integer.parseInt(a);
int num2 = Integer.parseInt(b);
System.out.println("Quotient : " + (num1/num2));
}
catch(IOException e)
{
System.out.println("IOException : " + e);
}
catch(NumberFormatException e)
{
System.out.println("NumberFormatException : " + e);
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException : " + e);
}
}
2
a
NumberFormatException : java.lang.NumberFormatException : For input string : "a"
tags: java , java program , java tutorialspoint , java language , tutorialspoint , exception handling , c , programs of c , c++ , programs of c++ , multiple catch statement , java code
At once just a single exception happens and at once just a single catch block is executed.
All catch blocks must be ordered from most explicit to most broad, for example catch for ArithmeticException must precede catch for Exception.
Java Program To Perform Multiple Catch Statement
import java.io.DataInputStream;
import java.io.IOException;
class MultipleCatch
{
public static void main(String args[])
{
DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter 2 numbers : ");
try
{
String a = d.readLine();
String b = d.readLine();
int num1 = Integer.parseInt(a);
int num2 = Integer.parseInt(b);
System.out.println("Quotient : " + (num1/num2));
}
catch(IOException e)
{
System.out.println("IOException : " + e);
}
catch(NumberFormatException e)
{
System.out.println("NumberFormatException : " + e);
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException : " + e);
}
}
OUTPUT:
Enter 2 numbers :2
a
NumberFormatException : java.lang.NumberFormatException : For input string : "a"
tags: java , java program , java tutorialspoint , java language , tutorialspoint , exception handling , c , programs of c , c++ , programs of c++ , multiple catch statement , java code