You will be given two integers x and y as input, you have to compute x/y . If x and y are not integers or if y is zero, exception will occur and you have to report it. You should use try and catch blocks to handle the exception. You also have to include a finally block to show output "Output from finally block". Read sample Input/Output to know what to report in case of exceptions.
10
3
3
Output from finally block
10
Hello
Check your input: java.util.InputMismatchException
Output from finally block
10
Value is undefined: java.lang.ArithmeticException: / by zero
Output from finally block
23.323
Check your input: java.util.InputMismatchException
Output from finally block
import java.util.InputMismatchException;
import java.util.Scanner;
public class MyMainClass {
public static void division(int x, int y) {
if(y == 0) {
try {
int rez = x / y;
} catch (ArithmeticException e) {
System.out.println("java.lang.ArithmeticException: / by zero");
}
} else {
System.out.println(x / y);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try {
int x = in.nextInt();
int y = in.nextInt();
MyMainClass.division(x, y);
} catch (InputMismatchException e) {
System.out.println("java.util.InputMismatchException");
}
}
}
Comments
Leave a comment