Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3613

FP in S1193 when the catch block contains more code

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.10
    • Component/s: Rules
    • Labels:
      None

      Description

      When a catch block contains code outside of the if statement performing the instanceof check, refactoring the code as the compliant example suggests would result in duplicating the same code. It is not cleaner, we should not report an issue.

      catch ( MyException e )
      {
          // Do non trivial exception handling.
          if ( e instanceof MySpecialExcetion ) // FP, refactoring this code is not at all cleaner
              doSomethingSpecialAdditionally();
          // Do more non trivial exception handling.
      }
      

      Note that we should make sure to not miss real issues, such as:

      catch ( MyException e )
      {
          if ( e instanceof MySpecialExcetion ) // Noncompliant
              throw new IllegalArgument(e);
          throw e;
      }
      

      But still, if the condition of the if is not a single instanceof, it makes sense to not report an issue, for example:

       } catch (Exception e) {
         if (e instanceof CanceledException && isCanceled()) { // Compliant
           return CANCEL;
         }
         return ERROR;
       }
      

      One strategy could be to report an issue only if:

      • The catch block contains only if statements, return and thow.
      • All the conditions of the if statements are single instanceof binary expressions.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              quentin.jaquier Quentin Jaquier
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: