70.division using throws keyword
//Division.java file
//Throws keyword
public class division
{
public void divide(int a, int b) throws ArithmeticException
{
int c = a / b;
System.out.println("Division : " + c);
}
}
================================================================
//calculate2.java
//throws keyword
import java.util.*;
class calculate2
{
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int x, y;
System.out.print("Enter Value X : ");
x = obj.nextInt();
System.out.print("Enter Value Y : ");
y = obj.nextInt();
try
{
division div = new division();
div.divide(x, y);
}
catch(ArithmeticException ae)
{
System.out.println("Second Argument must be Non-Zero");
}
}
}
OUTPUT:
Enter Value X : 12
Enter Value Y : 3
Division : 4
------------------------------------------------------------------------------------------
Enter Value X : 12
Enter Value Y : 0
Second Argument must be Non-Zero
Comments
Post a Comment