MULTIPLE CATCH BLOCK AND FINALLY BLOCKS IN JAVA
MULTIPLE CATCH BLOCK AND FINALLY BLOCKS IN JAVA

MULTIPLE CATCH BLOCK AND FINALLY BLOCKS IN JAVA

Multiple Catch:

Hello Friends, Today we learn about the concept of multiple catch block.T he concept of Multiple Catch And Finally Blocks is use When we not sure about which type of exception  .

FOR EXAMPLE:

try
{
int x,y,z;
System.out.println(“enter value x and y for division”);
z=x/y;
System.out.println(“Result of Division is”+z);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
catch(IOException i)
{
System.out.println(i);
}
In this example there is a chance of creation of many type of exception like AirthmeticException, IOException so we use their two catch block one to catch ArithmeticException and another Catch block is used to catch IOException.

Finally Block:
Java supports another block that is finally block which is used for display the massage or any type of statement. To use this block we have to add finally keyword. Finally block execute after the execution of try and catch block used to display the massage and different type of statement. Finally block is execute always either exception occurred or not.
FOR EXAMPLE:
try
{
//code which have to try
}
catch(Exception e)
{
//handling of exception
}
Finally
{
//code that is always execute either Exception is occurred or not
}

To start Coding we have to check:
1) Is java devolvement kit (jdk) is installed on your pc? If not then please install it first.
2) Is path variable set for java?
Now we can start Coding

CODE FOR MULTIPLE CATCH BLOCK AND FINALLY BLOCK:
import java.util.*;

class excep2
{

public static void main(String args[])
{
try
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println(“Enter values of x and y”);
x=s.nextInt();
y=s.nextInt();

z=x/y;

System.out.println(“Output:”+z);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
catch(Exception i)
{
System.out.println(i);
}
finally
{
System.out.println(“Finally block has been executed now……”);
}
}
}

:::::::::::::::::::::::: Step For Execution Of This Program:::::::::::::::::::::
1) Save file as Excep2.java.
2) Compile it by cmd as :- javac Excep2.java
3) Run it as :- java Excep2
4) After which your output will come

:::::::::::::::::::::::::::::::Output::::::::::::::::::::::::::::::::::

excep2

NO COMMENTS