Class ForbidThrowAnonymousExceptionsCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class ForbidThrowAnonymousExceptionsCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    This Check warns on throwing anonymous exception.

    Examples:
     catch (Exception e) {
            throw new RuntimeException()  { //WARNING
              //some code
         };
     }
     
    catch (Exception e) { RuntimeException run = new RuntimeException() { //some code }; throw run; //WARNING }
    The distinguishing of exception types occurs by analyzing variable's class's name.
    Check has an option which contains the regular expression for exception class name matching
    Default value is "^.*Exception" because usually exception type ends with suffix "Exception".
    Then, if we have an ObjBlock (distinguished by curly braces), it's anonymous
    exception definition. It could be defined in throw statement immediately.
    In that case, after literal new, there would be an expression type finishing with and ObjBlock.

    Since:
    1.11.0
    Author:
    Aleksey Nesterenko, Max Vetrenko
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Warning message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setExceptionClassNameRegex​(String exceptionClassNameRegex)
      Setter for pattern.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST literalThrowOrVariableDefAst)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • ForbidThrowAnonymousExceptionsCheck

        public ForbidThrowAnonymousExceptionsCheck()
    • Method Detail

      • setExceptionClassNameRegex

        public void setExceptionClassNameRegex​(String exceptionClassNameRegex)
        Setter for pattern.
        Parameters:
        exceptionClassNameRegex - The regular expression to set.
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST literalThrowOrVariableDefAst)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck