Java Rule S2142 and Catch Exception

549 views
Skip to first unread message

Austin

unread,
Apr 12, 2018, 9:13:32 AM4/12/18
to SonarQube

Hello,

First off, I'm new to posting here, greetings everyone!

So, Rule S2142 ("InterruptedException" should not be ignored) has been very handy for the various multi-threaded projects I work on. However, it doesn't seem to detect the following case.

Incorrect Code:

try
{        
    throwsException
();
    throwsInterruptedException
();
}
catch (Exception e)
{
    LOGGER
.error("An error occurred.", e);
   
return null; // Or throw e; etc...
   
// If e is an InterruptedException we need to re-interrupt... oops!
}


Correct Code:

try
{        
    throwsException
();
    throwsInterruptedException
();
}
catch (InterruptedException e)
{
    LOGGER
.debug("Interrupted");
   
Thread.currentThread().interrupt();
}
catch (Exception e)
{
    LOGGER
.error("An error occurred.", e);
    return null; // Or throw e; etc...
}


Code similar in pattern to this is fairly common for my projects due to interfaces, legacy code, and 3rd party code declaring methods that throw Exception. Is this something that rule S2142 should be updated to check? Or should a new rule be created for this? Or does this fall into the category of a custom rule I should write just for my SonarQube instance?

Thank You,

Austin

alexandr...@gmail.com

unread,
May 1, 2018, 4:25:30 AM5/1/18
to SonarQube
Hello Austin,

Welcome to the SonarQube community and sorry for the delay to get back to you.

I think the case you are describing should be handled by S2142 so I created https://jira.sonarsource.com/browse/SONARJAVA-2745 so we investigate it and find a way to catch it properly.
I suggest creating an account on https://jira.sonarsource.com so you can watch the ticket and be informed once it will be resolved.

Regards
Alex
Reply all
Reply to author
Forward
0 new messages