Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Change the MyException class below so that it does the following: a. The divide

ID: 3776219 • Letter: C

Question

Change the MyException class below so that it does the following:
a. The divide method throws an ArithmeticException with the message, “ArithmeticException - Cannot divide by zero”, if the variable, number2, is equal to zero.
b. Use try and catch blocks in the main method to catch the exception and print the message.
import java.util.*; public class MyException { public static int divide (int number1, int number2) { return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int number1 = input.nextInt(); int number2 = input.nextInt(); int result = divide (number1, number2); System.out.println(number1 + " / " + number2 + " is " + result); } } Change the MyException class below so that it does the following:
a. The divide method throws an ArithmeticException with the message, “ArithmeticException - Cannot divide by zero”, if the variable, number2, is equal to zero.
b. Use try and catch blocks in the main method to catch the exception and print the message.
import java.util.*; public class MyException { public static int divide (int number1, int number2) { return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int number1 = input.nextInt(); int number2 = input.nextInt(); int result = divide (number1, number2); System.out.println(number1 + " / " + number2 + " is " + result); } } Change the MyException class below so that it does the following:
a. The divide method throws an ArithmeticException with the message, “ArithmeticException - Cannot divide by zero”, if the variable, number2, is equal to zero.
b. Use try and catch blocks in the main method to catch the exception and print the message.
import java.util.*; public class MyException { public static int divide (int number1, int number2) { return number1 / number2; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter two integers: "); int number1 = input.nextInt(); int number2 = input.nextInt(); int result = divide (number1, number2); System.out.println(number1 + " / " + number2 + " is " + result); } }

Explanation / Answer

Hi,

I have implemented as per your requirement and highlighted the code changes below.

MyException.java

import java.util.*;

public class MyException {
   public static int divide(int number1, int number2) {
    if(number2 == 0){
           throw new ArithmeticException("ArithmeticException - Cannot divide by zero");
       }
    return number1 / number2;
   }

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter two integers: ");
       int number1 = input.nextInt();
       int number2 = input.nextInt();
    try{
    int result = divide(number1, number2);
       System.out.println(number1 + " / " + number2 + " is " + result);
    }
       catch(ArithmeticException e){
           System.out.println(e.getMessage());
       }
}
}

Output:

Enter two integers: 2 0
ArithmeticException - Cannot divide by zero

Enter two integers: 4 2
4 / 2 is 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote