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...

Modern networks primarily use three types of media to interconnect devices. What are these three types?

What will be an ideal response?

Computer Science & Information Technology

Case 13-2Jarrod is using Java to write binary search algorithms.Jarrod knows that if the length of his list is 4 to 7, the maximum number of steps needed to do a binary search is ____.

A. 1 B. 3 C. 4 D. 7

Computer Science & Information Technology