Class NoCauseGetMessageInExceptionConstructorCheck

All Implemented Interfaces:
Configurable, Contextualizable

Checks that throw statements within catch blocks do not use the caught exception's getMessage() call when the thrown exception is the same type as the caught exception, as rethrowing the same exception type with its own message is redundant.

Rationale: When throwing an exception of the same type as the caught exception and including the caught exception's message via getMessage(), the information is redundant. The original exception should be rethrown directly, or a different exception type should be used.

Example of violations:

catch (IOException ex) {
    throw new IOException("Error: " + ex.getMessage()); // violation
}

Correct usage:

catch (IOException ex) {
    throw new RuntimeException("Error: " + ex.getMessage(), ex); // OK, different type
}
catch (IOException ex) {
    throw new IOException("Error processing file", ex); // OK, no getMessage()
}
Since:
14.4.0
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.
      Specified by:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • beginTree

      public void beginTree(DetailAST rootAST)
      Description copied from class: AbstractCheck
      Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
      Overrides:
      beginTree in class AbstractCheck
      Parameters:
      rootAST - the root of the tree
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • leaveToken

      public void leaveToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called after all the child nodes have been process.
      Overrides:
      leaveToken in class AbstractCheck
      Parameters:
      ast - the token leaving
    • checkThrow

      private void checkThrow(DetailAST throwAst)
      Reports the throw statement if it references a matching caught exception's getMessage() inside its expression.
      Parameters:
      throwAst - the LITERAL_THROW AST node
    • getThrownType

      private static String getThrownType(DetailAST throwAst)
      Gets the simple type name of the exception being thrown via a new expression.
      Parameters:
      throwAst - the LITERAL_THROW AST node
      Returns:
      the simple type name, or null if the throw does not use a new expression
    • hasViolatingGetMessage

      private boolean hasViolatingGetMessage(DetailAST ast, String thrownType)
      Recursively checks if the AST subtree contains a getMessage() call on an enclosing caught exception whose declared type matches the thrown type.
      Parameters:
      ast - the AST node to search
      thrownType - the simple type name of the thrown exception
      Returns:
      true if a violating call is found
    • isViolatingGetMessage

      private boolean isViolatingGetMessage(DetailAST node, String thrownType)
      Checks if a single node is <var>.getMessage() where var is declared by any enclosing catch whose type matches the thrown type.
      Parameters:
      node - the AST node to check
      thrownType - the simple type name of the thrown exception
      Returns:
      true if this is a matching call
    • matchesCatchScope

      private static boolean matchesCatchScope(DetailAST catchBlock, String varName, String thrownType)
      Checks whether the given catch block declares the specified variable and catches a type matching the thrown type.
      Parameters:
      catchBlock - the LITERAL_CATCH AST node
      varName - the caught-exception variable name to match
      thrownType - the simple type name of the thrown exception
      Returns:
      true if the catch matches both the variable and the type
    • containsTypeName

      private static boolean containsTypeName(DetailAST ast, String typeName)
      Recursively checks if the given AST contains a type matching the specified name. Handles IDENT (simple names), DOT (qualified names), and BOR (multi-catch) nodes.
      Parameters:
      ast - the AST node to search
      typeName - the type name to match
      Returns:
      true if a matching type is found