import java.util.*;

public class DivisionMistakeCaught3
{
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
       int numerator, denominator, result;
       try
       {
          System.out.print("Enter numerator >> ");
          numerator = input.nextInt();
          System.out.print("Enter denominator >> ");
          denominator = input.nextInt();
          result = numerator / denominator;
          System.out.println(numerator + " / " + denominator + " = " + result);
       }
       catch(ArithmeticException mistake)
       {
          System.out.println(mistake.getMessage());
       }
       catch(InputMismatchException mistake)
       {
          System.out.println("Wrong data type");
       }  
    }
}
?
Using the above code, describe what will happen if a user enters two usable integers. What will happen if a user enters an invalid noninteger value? What will happen if the user enters 0 for the denominator?

What will be an ideal response?


If the user enters two usable integers, the result is calculated, normal output is displayed, and neither catch block executes.?If the user enters an invalid (noninteger) value for either the numerator or the denominator, an InputMismatchException object is created and thrown. When the program encounters the first catch block (that catches an ArithmeticException), the block is bypassed because the Exception types do not match. When the program encounters the second catch block, the types match, and the "Wrong data type" message is displayed.?If the user enters 0 for denominator, the division statement throws an ArithmeticException, and the try block is abandoned. When the program encounters the first catch block, the Exception types match, the value of the getMessage() method is displayed, and then the second catch block is bypassed.

Computer Science & Information Technology

You might also like to view...

Write a method that will play half of one sound and then the second half of the first sound added to the first half of another sound and then the last half of the second sound. Use Program 80 as a starting point.

What will be an ideal response?

Computer Science & Information Technology

Which of the following is NOT a form control?

A) Drop-Down List Content B) Chart Content C) Rich Text Content D) Date Picker Content

Computer Science & Information Technology

Platform as a Service (PaaS)

What will be an ideal response?

Computer Science & Information Technology

Systems that are used by an entire enterprise are referred to as ____________________ systems.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology