Change unhandled exception auto-generated catch code in Eclipse?

前端 未结 4 704
终归单人心
终归单人心 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.

    0 讨论(0)
  • 2020-12-14 07:26

    Yes, you can change the default code added by Eclipse.

    1. In Preferences, navigate to Java>Code Style>Code Templates.
    2. Under Code, select Catch block body.
    3. Press the Edit button to change the code. When finished, press the OK button.

    Consider adding a TODO comment in the default catch block. For example, the default includes:

         // ${todo} Auto-generated catch block
    
    0 讨论(0)
  • 2020-12-14 07:26

    If you are re-throwing your exception from the catch clause, then you would have to handle in the method that invoked your current method. But if you wrap your exception in RuntimeException, you won't need to handle it. But why would you do that?

    I mean why not just: -

    try {
       myfunction();
    } catch (MyUnhandledException e) {
        throw e;
    }
    

    Because, in your code, basically you are wrapping a might be checked exception in an unchecked one. If I assume your MyUnhandledException as checked exception.

    And also note that, if you are following this approach, you would still need to declare it to be thrown in your throws clause.

    If you just want to do the way you are doing, then also it will work fine. You can change the Eclipse setting as per @Andy's answer.

    But, it would be better to look at your design. Why is the method overrided throwing an exception not declared in your overriden method. Probably there is something wrong, that should be corrected.

    0 讨论(0)
  • 2020-12-14 07:33

    You're probably aware of this... but if you want to get rid of all pesky clutter and irritations from checked exceptions, why not just add throws Exception to every single method?

    In the case of an overridden interface method this sort of pattern could then be used:

    @Override
    public void close() throws IOException {
        try {
            _close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        }
    }
    
    private void _close() throws Exception {
        // ... closing ops
    }
    
    0 讨论(0)
提交回复
热议问题