Answer to Question #323032 in Java | JSP | JSF for shahriar

Question #323032

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

1
Expert's answer
2022-04-03T16:10:15-0400
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");
        }

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS