请将上面的实例的异常处理方法改为throws
时间: 2023-05-29 16:06:40 浏览: 77
public class Calculator {
public static int divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("除数不能为0");
}
return a / b;
}
public static void main(String[] args) {
try {
int result = divide(6, 0);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("捕获异常:" + e.getMessage());
}
}
}
阅读全文