【发布时间】:2021-07-24 18:00:55
【问题描述】:
我想问是否可以阻止 IllegalArgumentException 破坏程序运行。如果我这样做:
throw new
IllegalArgumentException("Value not
allowed!");
continue;
这会阻止 IAE 在异常触发后中断程序吗?如果没有,有什么方法可以将其作为错误消息发送给用户,然后允许他们继续运行程序而无需重新运行?
【问题讨论】:
-
打印一条消息然后:
System.err.println("Value not allowed!"); -
抛出异常的目的是如果抛出的错误没有得到处理,程序就会崩溃。如果您不希望程序因错误而崩溃,您可能不应该抛出异常,或者您应该在调用链的其他位置捕获它。
-
阅读 try-catch 块。
-
throw表示“现在停止并返回调用堆栈”。
标签: java break illegalargumentexception continue