Change unhandled exception auto-generated catch code in Eclipse?

前端 未结 4 721
终归单人心
终归单人心 2020-12-14 06:47

If I have unhandled exception in Java, Eclipse proposes two options to me: (1) add throws declaration and (2) surround with try/catch.

If I choose (2) it adds a code

4条回答
  •  被撕碎了的回忆
    2020-12-14 07:23

    Personally, I use a generic idiom irrespective of the actual checked exception type, you might make Eclipse use that as a template instead:

    try {
     ...
    } 
    catch (RuntimeException e) { throw e; } 
    catch (Exception e) { throw new RuntimeException(e); }
    

    The point is to wrap the whole code block instead of individually each line that may throw an exception. The block may throw any number of checked and unchecked exceptions, and this will allow the unchecked exceptions to pass through unharmed, and the checked exceptions will be wrapped.

提交回复
热议问题