View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Set;
25  import java.util.stream.Collectors;
26  
27  import com.puppycrawl.tools.checkstyle.StatelessCheck;
28  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
29  import com.puppycrawl.tools.checkstyle.api.DetailAST;
30  import com.puppycrawl.tools.checkstyle.api.FullIdent;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
33  
34  /**
35   * <div>
36   * Checks that certain exception types do not appear in a {@code catch} statement.
37   * </div>
38   *
39   * <p>
40   * Rationale: catching {@code java.lang.Exception}, {@code java.lang.Error} or
41   * {@code java.lang.RuntimeException} is almost never acceptable.
42   * Novice developers often simply catch Exception in an attempt to handle
43   * multiple exception classes. This unfortunately leads to code that inadvertently
44   * catches {@code NullPointerException}, {@code OutOfMemoryError}, etc.
45   * </p>
46   *
47   * @since 3.2
48   */
49  @StatelessCheck
50  public final class IllegalCatchCheck extends AbstractCheck {
51  
52      /**
53       * A key is pointing to the warning message text in "messages.properties"
54       * file.
55       */
56      public static final String MSG_KEY = "illegal.catch";
57  
58      /** Specify exception class names to reject. */
59      private final Set<String> illegalClassNames = Arrays.stream(new String[] {"Exception", "Error",
60          "RuntimeException", "Throwable", "java.lang.Error", "java.lang.Exception",
61          "java.lang.RuntimeException", "java.lang.Throwable", })
62              .collect(Collectors.toCollection(HashSet::new));
63  
64      /**
65       * Setter to specify exception class names to reject.
66       *
67       * @param classNames
68       *            array of illegal exception classes
69       * @since 3.2
70       */
71      public void setIllegalClassNames(final String... classNames) {
72          illegalClassNames.clear();
73          illegalClassNames.addAll(
74                  CheckUtil.parseClassNames(classNames));
75      }
76  
77      @Override
78      public int[] getDefaultTokens() {
79          return getRequiredTokens();
80      }
81  
82      @Override
83      public int[] getRequiredTokens() {
84          return new int[] {TokenTypes.LITERAL_CATCH};
85      }
86  
87      @Override
88      public int[] getAcceptableTokens() {
89          return getRequiredTokens();
90      }
91  
92      @Override
93      public void visitToken(DetailAST detailAST) {
94          final DetailAST parameterDef =
95              detailAST.findFirstToken(TokenTypes.PARAMETER_DEF);
96          final DetailAST excTypeParent =
97                  parameterDef.findFirstToken(TokenTypes.TYPE);
98  
99          DetailAST currentNode = excTypeParent.getFirstChild();
100         while (currentNode != null) {
101             final FullIdent ident = FullIdent.createFullIdent(currentNode);
102             final String identText = ident.getText();
103             if (illegalClassNames.contains(identText)) {
104                 log(detailAST, MSG_KEY, identText);
105             }
106             currentNode = currentNode.getNextSibling();
107         }
108     }
109 }