简体   繁体   中英

IllegalArgumentException + continue to avoid breaking program?

I want to ask if it is possible to stop a IllegalArgumentException from breaking the program running. If I do:

throw new 
IllegalArgumentException("Value not 
allowed!");
continue;

Will this stop the IAE from breaking the program after the exception is triggered? If not, is there any way to just throw this as an error message to the user and then allow them to continue running the program without having to re-run?

  1. If you want to handle an exception (ie stop the program from terminating), you must use a try/catch block.

  2. Whenever an exception is thrown, control exits immediately. Your "continue" statement is NOT executed.

  3. When you "catch" an exception, you may re-throw it. You'll presumably have another try/catch block at some higher level in the code.

  4. Your BEST strategy, however, is to "code defensively" and try to prevent the IllegalArgumentException from occurring in the first place.

  5. Finally, you NEVER want to LOSE information.

     /* * 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!");

要么使用try..catch结构来“拦截”异常,要么一开始就不要抛出异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM