Java program to read two integers a and b. Compute a/b and print, when b is not zero. Raise an exception when b is equal to zero.

				
					import java.util.Scanner;
class division
{
public static void main(String[] args)
{
int a,b,result;
Scanner input =new Scanner(System.in);
System.out.println("Input two integers");
a=input.nextInt();
b=input.nextInt();
try
{
result=a/b;
System.out.println("Result="+result);
}
catch(ArithmeticException e)
{
System.out.println("exception caught: Divide by zero
error"+e);
}
}
				
			
Output:
Input two integers
8  2
Result=4
Input two integers
6   0
exception caught: Divide by zero

Leave a Reply