Change unhandled exception auto-generated catch code in Eclipse?

前端 未结 4 723
终归单人心
终归单人心 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: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
    }
    

提交回复
热议问题