Write a Program for exception Handling in java

 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.






Comments

Popular posts from this blog

Java Project Code: Cinema Ticket booking system : Create a Java Application to book a ticket in a Cinema hall.