Write a program ―DivideByZeroǁ that takes two numbers a and b as input,
computes a/b, and invokes Arithmetic Exception to generate a message when the
denominator is zero.
import java.util.Scanner;
public class DivideByZero {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
double a=input.nextDouble();
System.out.print("Enter a: ");
double b=input.nextDouble();
try{
double result =a/b;
System.out.println("a/b = "+a+"/"+b+" = "+result);
}catch(ArithmeticException ex){
System.out.println("Division by zero");
}
input.close();
}
}
Comments
Leave a comment