我刚接触Java,遇到了抛出异常的问题。具体来说,为什么这样是不正确的呢?
public static void divide(double x, double y){
if(y == 0){
throw new Exception("Cannot divide by zero.");
//Generates error message that states the exception type is unhanded
}
else
System.out.println(x + " divided by " + y + " is " + x/y);
//other code follows
}
但这可以吗?
public static void divide(double x, double y){
if(y == 0)
throw new ArithmeticException("Cannot divide by zero.");
else
System.out.println(x + " divided by " + y + " is " + x/y);
//other code follows
}