为什么我不需要在方法 [Java] 中声明异常?

问题描述

在下面的代码中::

README

https://www.w3schools.com/java/java_try_catch.asp

我不应该添加投掷到 public class Main { static void checkAge(int age) { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { System.out.println("Access granted - You are old enough!"); } } public static void main(String[] args) { checkAge(19); checkAge(17); } } ?


此外,如果我将 static void checkAge(int age) throws ArithmeticException{ 更改为 ArithmeticException,那么我必须使用 Exception。为什么?

throw

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)