Gle*_*ari 0 java continue break illegalargumentexception
我想问一下是否有可能阻止 IllegalArgumentException 中断程序运行。如果我做:
throw new
IllegalArgumentException("Value not
allowed!");
continue;
Run Code Online (Sandbox Code Playgroud)
触发异常后,这会阻止IAE破坏程序吗?如果没有,是否有任何方法可以将其作为错误消息抛出给用户,然后允许他们继续运行程序而无需重新运行?
如果要处理异常(即阻止程序终止),则必须使用try/catch块。
每当抛出异常时,控制立即退出。是你的“继续”语句不执行。
当您“捕获”异常时,您可以重新抛出它。您可能会在代码中的某个更高级别有另一个 try/catch 块。
但是,您最好的策略是“防御性编码”并首先尝试防止 IllegalArgumentException 发生。
最后,您永远不想丢失信息。
/*
* Poor:
* *WHAT* value??? Where did this occur? Why?
* You've just lost all this information, "Value not allowed" is uselessly vague...
*/
throw new IllegalArgumentException("Value not allowed!");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33 次 |
| 最近记录: |