Program for Exception Handling in Java using try and catch blocks
1. Open the notepad file.
2. Write the code given below in the file.
import java.util.Scanner;
class Excep
{
public static void main(String args[])
{
int div,a,b;
Scanner input= new Scanner(System.in);
System.out.println("Enter 1st Number");
a=input.nextInt();
System.out.println("Enter 2nd Number");
b=input.nextInt();
try
{
div=a/b;
System.out.println("Division of numbers: "+div);
}
catch(Exception e)
{
System.out.println("Exception is caught.");
System.out.println("\nDivision by zero.");
}
finally
{
System.out.println("This is a program of exception handling.");
}
}
}
3. Now save the file with name Excep.java.
1. Open the notepad file.
2. Write the code given below in the file.
import java.util.Scanner;
class Excep
{
public static void main(String args[])
{
int div,a,b;
Scanner input= new Scanner(System.in);
System.out.println("Enter 1st Number");
a=input.nextInt();
System.out.println("Enter 2nd Number");
b=input.nextInt();
try
{
div=a/b;
System.out.println("Division of numbers: "+div);
}
catch(Exception e)
{
System.out.println("Exception is caught.");
System.out.println("\nDivision by zero.");
}
finally
{
System.out.println("This is a program of exception handling.");
}
}
}
3. Now save the file with name Excep.java.
No comments:
Post a Comment