SonarQube Java
  1. SonarQube Java
  2. SONARJAVA-29

"Avoid Throwing Raw Exception Types" Rule not able to detect "throw new Exception" in all cases

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3
    • Component/s: PMD
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      1

      Description

      Here is a dummy class where we try to confirm SONAR is able to detect bad usage of "throw new Exception":

      public class ExceptionTest {
      
        public int buildTransferAgentTrade(String param1, String param2) throws Exception, Throwable {
      
          boolean myValue = true;
          if (myValue) {
            throw new RuntimeException();
          }
          myValue = true;
          if (myValue) {
            throw new Exception();
          }
          myValue = true;
          if (myValue) {
            throw new Exception("a value");
          }
          try {
          } catch (Exception e) {
            throw new Exception("Error while loading trades", e);
          }
          myValue = true;
          if (myValue) {
            throw new Throwable();
          }
          myValue = true;
          if (myValue) {
            throw new Error();
          }
          try {
            String nothing = "notGood";
            if ("notGood".equalsIgnoreCase(nothing)) {
              throw new RemoteException("notGood");
            }
          } catch (RemoteException e) {
            throw new Exception(e);
          }
      
          return 1;
        }
      }
      

      When we run the analysis, SONAR is not able to detect the 3 "throw new Exception" whereas in some case we have seen it was able to.
      To confirm that please have a look at the attached screenshot.

      In the documentation of the rule "Avoid Throwing Raw Exception Types" it's written:

      Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead.

      That's why in our test class, you will find use case of RuntimeException, Throwable, Exception and Error.

        Issue Links

          Activity

          Hide
          Evgeny Mandrikov added a comment -

          For the record: PMD is not able to detect "throw new Exception" in case if there is import, which ends with "Exception", i.e. in given example - "import java.rmi.RemoteException".

          Show
          Evgeny Mandrikov added a comment - For the record: PMD is not able to detect "throw new Exception" in case if there is import, which ends with "Exception", i.e. in given example - "import java.rmi.RemoteException".
          Hide
          Evgeny Mandrikov added a comment -

          Fixed by SONARJAVA-125.

          Show
          Evgeny Mandrikov added a comment - Fixed by SONARJAVA-125 .
          Hide
          Fabrice Bellingard added a comment - - edited

          Rule should be removed from default profiles.

          Show
          Fabrice Bellingard added a comment - - edited Rule should be removed from default profiles.
          Hide
          Evgeny Mandrikov added a comment -

          Done.

          Show
          Evgeny Mandrikov added a comment - Done.
          Hide
          Fabrice Bellingard added a comment -

          Checked.

          Show
          Fabrice Bellingard added a comment - Checked.
          Hide
          Evgeny Mandrikov added a comment -

          Integration tests were updated.

          Show
          Evgeny Mandrikov added a comment - Integration tests were updated.

            People

            • Assignee:
              Evgeny Mandrikov
              Reporter:
              Alexandre GIGLEUX
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: