New Check: Useless Catch Check #267

Closed
baratali opened this Issue Oct 30, 2014 · 4 comments

Projects

None yet

3 participants

@baratali
Contributor

Forbid catch blocks that just throw exception without wrapping.

try {
    ...
}
catch(UnsupportedOperationException e) { // warning
    throw e;
}
catch(Exception e) { // no warning
    throw new RuntimeException(e);
}
@alex-zuy alex-zuy was assigned by daniilyar Nov 25, 2014
@alex-zuy
Contributor

Consider following example

public class User
{
    static class Ex extends Exception { }

    static class Ex1 extends Ex { }
    static class Ex2 extends Ex { }
    static class Ex3 extends Ex { }

    public void run() throws Exception
    {
        try {
            ....
        }
        catch(Ex1 e) {
            throw e;
        }
        catch(Ex e) {
            e.printStackTrace();
        }
    }
}

try block can throw exceptions of type Ex, Ex1, Ex2, Ex3. User dont want to handle exception of type Ex1 in run() method, but wants to handle all another exceptions of type Ex and it derivatives.
In this case first catch block is not useless. Checkstyle gives no opportunity to determine type hierarcy of classes, so we cannot distinguish such cases. This means that check should not look for multi-catch cases and only consider sigle-catch ones.

@romani
Member
romani commented Nov 26, 2014

I do not any way for Checkstyle to validate that code correctly, tools with type awareness need to do that.

@romani romani closed this Nov 26, 2014
@alex-zuy
Contributor

Check can handle single-catch cases correctly without any type information.

@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 26, 2014
@alex-zuy alex-zuy new check UselessCatchCheck implemented. issue #267 fixed 99cb81e
@alex-zuy
Contributor

Sevntu checkstyle google group thread

@romani romani reopened this Nov 26, 2014
@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 26, 2014
@alex-zuy alex-zuy new check UselessSingleCatchCheck implemented. issue #267 fixed 0e62c71
@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 27, 2014
@alex-zuy alex-zuy new check UselessSingleCatchCheck implemented. issue #267 fixed ce9b230
@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 27, 2014
@alex-zuy alex-zuy new check UselessSingleCatchCheck implemented. issue #267 fixed b22ade5
@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 29, 2014
@alex-zuy alex-zuy new check UselessSingleCatchCheck implemented. issue #267 fixed 9038db9
@alex-zuy alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Dec 1, 2014
@alex-zuy alex-zuy new check UselessSingleCatchCheck implemented. issue #267 fixed 33a6f2e
@romani romani closed this Dec 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment