For a homework assignment, I have to create a simple program that takes two numbers as arguments, and multiplies them. If one of the numbers is a zero, the program throws an ArithmeticException
It always seemed to me when I read the documentation however that AritmeticException only handles divide by zero errors and other things that are impossible by math. However, the assignment wants this built-in handler to do the work, so how do you get it to accept a multiply by zero as an error?
My code so far (which is coded to only handle divide by zero and other “standard” math errors)
public class MultTwo {
public static void main(String[] args) {
try {
int firstNum = Integer.parseInt(args[0]);
int secondNum = Integer.parseInt(args[1]);
System.out.println(firstNum*secondNum);
}
catch (ArithmeticException a) {
System.out.println("You're multplying by zero!");
}
}//end main
}//end MultTwo Class
What about
Though this is not a good practice I guess your teacher wants to show you something with it.