title: Sonarqube - Catches should be combined
body: <p>Let me start by stating that I have read <a href="https://stackoverflow.com/questions/49065496/sonarqube-avoiding-catch-generic-exception">this post</a> which does not address my particular issue.</p>
  
  <p>According to <a href="https://rules.sonarsource.com/java/RSPEC-2147" rel="nofollow noreferrer">SonarQube rule (java:S2147)</a> - Catches should be combined.</p>
  
  <p>I tested my code <strong>without</strong> my exceptions combined and <strong>with</strong> my exceptions combined but I am get a behaviour that I did not expect. </p>
  
  <p>Here's my test code: </p>
  
  <ul>
  <li>BusinessFault </li>
  <li>SystemException </li>
  <li>ExceptionUtility </li>
  <li>ExceptionTester (incl. main method)</li>
  </ul>
  
  <pre><code>public class BusinessFault extends Exception {
      public BusinessFault(String message) {
          super(message);
      }
  }
  </code></pre>
  
  <pre><code>public class SystemException extends Exception {
      public SystemException(String message) {
          super(message);
      }
  }
  </code></pre>
  
  <pre><code>public class ExceptionUtility {
      private ExceptionUtility() {
          // private no args constructor
      }
  
      public static void logException(Logger logger, BusinessFault bf) {
           logger.log(Level.SEVERE, " BUSINESS FAULT THROWN {0}", bf.getMessage());
      }
  
      public static void logException(Logger logger, SystemException se) {
          logger.log(Level.SEVERE, " SYSTEM FAULT THROWN {0}", se.getMessage());
      }
  
      public static void logException(Logger logger, Exception e) {
          logger.log(Level.SEVERE, " GENERAL EXCEPTION {0}", e.getMessage());
      }
  }
  </code></pre>
  
  <pre><code>public class ExceptionTester {
  
      private static final Logger logger = Logger.getLogger(ExceptionTester.class.getName());
  
      public static void main(String[] args) {
  
          ExceptionTester et = new ExceptionTester();
  
          try {
              et.throwMyCustomException("SE");
  //          et.throwMyCustomException("BF");
          } catch (BusinessFault e) {
              logger.log(Level.SEVERE, " ##### (ExceptionTester) exception caught - msg: {0} ", e.getMessage());
              ExceptionUtility.logException(logger, e);
          } catch (SystemException e) {
              logger.log(Level.SEVERE, " ##### (ExceptionTester) exception caught - msg: {0} ", e.getMessage());
              ExceptionUtility.logException(logger, e);
          } catch (Exception e) {
              ExceptionUtility.logException(logger, e);
          }
      }
  
      public void throwMyCustomException(String exceptionType) throws SystemException, BusinessFault {
  
          System.out.println(" ::::::::::: throwMyCustomException() : " + exceptionType);
  
          if (exceptionType.equals("SE")) {
              throw new SystemException("  &gt; **** SYSTEM EXCEPTION thrown... **** ");
          } else if (exceptionType.equals("BF")) {
              throw new BusinessFault("  &gt; **** BUSINESS FAULT thrown... **** ");
          }
      }
  }
  </code></pre>
  
  <p>When I throw my SystemException <strong>without</strong> BusinessFault and SystemException combined in the same catch statement, I get the behaviour that I expect which is the following method being called.</p>
  
  <pre><code>// in ExceptionUtility
  
  public static void logException(Logger logger, SystemException se) {
      logger.log(Level.SEVERE, " SYSTEM FAULT THROWN {0}", se.getMessage());
  }
  </code></pre>
  
  <p>Console output:</p>
  
  <pre><code> ::::::::::: throwMyCustomException() : SE
  May 12, 2020 19:03:56 AM com.exceptions.ExceptionTester main
  SEVERE:  ##### (ExceptionTester) exception caught - msg:   &gt; **** SYSTEM EXCEPTION thrown... ****  
  May 12, 2020 19:03:56 AM com.exceptions.ExceptionUtility logException
  SEVERE:  SYSTEM FAULT THROWN   &gt; **** SYSTEM EXCEPTION thrown... **** 
  </code></pre>
  
  <p>But when I combine the BusinessFault and SystemException in the same catch statement as shown below and throw the SystemException, I get a different behaviour.</p>
  
  <pre><code>// in ExceptionTester
  
  try {
      et.throwMyCustomException("SE");
      //et.throwMyCustomException("BF");
  } catch (BusinessFault | SystemException e) {  // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; COMBINED EXCEPTIONS
      logger.log(Level.SEVERE, " ##### (ExceptionTester) exception caught - msg: {0} ", e.getMessage());
      ExceptionUtility.logException(logger, e);
  } catch (Exception e) {
      ExceptionUtility.logException(logger, e);
  }
  
  </code></pre>
  
  <p>A different method in my ExceptionUtility is called instead. </p>
  
  <pre><code>// in ExceptionUtility
  
  public static void logException(Logger logger, Exception e) {
      logger.log(Level.SEVERE, " GENERAL EXCEPTION {0}", e.getMessage());
  }
  
  </code></pre>
  
  <p>Console output: </p>
  
  <pre><code> ::::::::::: throwMyCustomException() : SE
  May 12, 2020 19:10:30 AM com.exceptions.ExceptionTester main
  SEVERE:  ##### (ExceptionTester) exception caught - msg:   &gt; **** SYSTEM EXCEPTION thrown... ****  
  May 12, 2020 19:10:30 AM com.exceptions.ExceptionUtility logException
  SEVERE:  GENERAL EXCEPTION   &gt; **** SYSTEM EXCEPTION thrown... **** 
  </code></pre>
  
  <p>I can see that it's still the SystemException message being passed. But that's not really the method that I need to be called. For simplicity reasons, my logExceptions only log info. But my need is to handle  SystemException and BusinessFault differently from a generic Exception. </p>
  
  <p>Can anybody please shed some slight on why this behaviour occurs? More specifically, why the logException method for handling SystemException in ExceptionUtility class is not called in both cases? </p>