Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Check: Useless Catch Check #267

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

New Check: Useless Catch Check #267

baratali opened this issue Oct 30, 2014 · 4 comments
Assignees

Comments

@baratali
Copy link
Contributor

@baratali baratali commented Oct 30, 2014

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
Copy link
Contributor

@alex-zuy alex-zuy commented Nov 25, 2014

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
Copy link
Member

@romani 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
Copy link
Contributor

@alex-zuy alex-zuy commented Nov 26, 2014

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

alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 26, 2014
@alex-zuy
Copy link
Contributor

@alex-zuy alex-zuy commented Nov 26, 2014

Sevntu checkstyle google group thread

@romani romani reopened this Nov 26, 2014
alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 26, 2014
alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 27, 2014
alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 27, 2014
alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Nov 29, 2014
alex-zuy added a commit to alex-zuy/sevntu.checkstyle that referenced this issue Dec 1, 2014
romani added a commit that referenced this issue Dec 3, 2014
@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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.