Repository: checkstyle/checkstyle

Problem statement:
WhitespaceAfter should support LITERAL_CATCH token
https://checkstyle.org/config_whitespace.html#WhitespaceAfter

I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

**How it works Now:**

```bash
/var/tmp $ javac CatchSpace.java
/var/tmp $

/var/tmp $ cat config.xml
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="WhitespaceAfter">
            <property name="tokens" value="LITERAL_CATCH"/>
        </module>
    </module>
</module>

/var/tmp $ cat CatchSpace.java
public class CatchSpace {
    public static void main(String[] args) {
        try {
            Integer.parseInt(args[0]);
        } catch(NumberFormatException e) {
            System.out.println("not a number");
        }
    }
}


/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.1-all.jar -c config.xml CatchSpace.java
com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Token "LITERAL_CATCH" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:476)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:403)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:330)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:189)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:126)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Token "LITERAL_CATCH" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck
        at com.puppycrawl.tools.checkstyle.TreeWalker.registerCheck(TreeWalker.java:223)
        at com.puppycrawl.tools.checkstyle.TreeWalker.setupChild(TreeWalker.java:133)
        at com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:201)
        at com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:471)
        ... 5 more
Checkstyle ends with 1 errors.
```

**Is your feature request related to a problem? Please describe.**
It would be nice if checkstyle could detect missing space after `catch` keyword.

**Describe the solution you'd like**
WhitespaceAfter could support new literal type - `LITERAL_CATCH`

**Additional context**
Google Java Style Guide requires whitespace separation of `(` from any reserved words
See https://google.github.io/styleguide/javaguide.html#s4.6.2-horizontal-whitespace
>1. Separating any reserved word, such as `if`, `for` or `catch`, from an open parenthesis (`(`) that follows it on that line

#11489, #11493 and #11494 are similar requests for `try`/`finally`/`synchronized` keywords support


Hints:
I am on it.

Patch:
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheck.java
@@ -56,6 +56,8 @@
  * LITERAL_FOR</a>,
  * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FINALLY">
  * LITERAL_FINALLY</a>,
+ * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">
+ * LITERAL_CATCH</a>,
  * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">
  * DO_WHILE</a>,
  * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ELLIPSIS">
@@ -100,6 +102,9 @@
  *
  *      try {} catch (Error e){} finally {} // OK
  *      try {} catch (Error e){} finally{} // violation ''finally' is not followed by whitespace'
+ *
+ *      try {} catch (Exception e){} // OK
+ *      try {} catch(Exception e){} // violation ''catch' is not followed by whitespace'
  *  }
  * </pre>
  * <p>
@@ -172,6 +177,7 @@ public int[] getAcceptableTokens() {
             TokenTypes.LITERAL_DO,
             TokenTypes.LITERAL_FOR,
             TokenTypes.LITERAL_FINALLY,
+            TokenTypes.LITERAL_CATCH,
             TokenTypes.DO_WHILE,
             TokenTypes.ELLIPSIS,
             TokenTypes.LITERAL_SWITCH,


Test patch:
diff --git a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAfterTest.java b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAfterTest.java
--- a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAfterTest.java
+++ b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/WhitespaceAfterTest.java
@@ -59,6 +59,7 @@ public void testWhitespaceAfterBad() throws Exception {
             "31:9: " + getCheckMessage(clazz, message, "try"),
             "35:16: " + getCheckMessage(clazz, message, "finally"),
             "36:38: " + getCheckMessage(clazz, message, "finally"),
+            "40:16: " + getCheckMessage(clazz, message, "catch"),
         };
         final Configuration checkConfig = getModuleConfig("WhitespaceAfter");
         final String filePath = getPath("InputWhitespaceAfterBad.java");
diff --git a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterBad.java b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterBad.java
--- a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterBad.java
+++ b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterBad.java
@@ -35,4 +35,8 @@ public void check5() {
         try {} finally{} // warn
         try {} catch (Exception e){} finally{} // warn
     }
+
+    public void check6() {
+        try {} catch(Exception e){} // warn
+    }
 }
diff --git a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterGood.java b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterGood.java
--- a/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterGood.java
+++ b/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule462horizontalwhitespace/InputWhitespaceAfterGood.java
@@ -29,11 +29,15 @@ public void check3(int... a) {
 
     public void check4() throws java.io.IOException {
         try (java.io.InputStream ignored = System.in) {}
-        try {} catch(Exception e){}
+        try {} catch (Exception e){}
     }
 
     public void check5() {
         try {} finally {}
         try {} catch (Exception e){} finally {}
     }
+
+    public void check6() {
+        try {} catch (Exception e){}
+    }
 }
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/WhitespaceAfterCheckTest.java
@@ -170,6 +170,16 @@ public void testLiteralTry() throws Exception {
             expected);
     }
 
+    @Test
+    public void testLiteralCatch() throws Exception {
+        final String[] expected = {
+            "14:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "catch"),
+        };
+        verifyWithInlineConfigParser(
+            getPath("InputWhitespaceAfterLiteralCatch.java"),
+            expected);
+    }
+
     @Test
     public void testEmptyForIterator() throws Exception {
         final String[] expected = {
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/whitespaceafter/InputWhitespaceAfterLiteralCatch.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/whitespaceafter/InputWhitespaceAfterLiteralCatch.java
new file mode 100644
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/whitespaceafter/InputWhitespaceAfterLiteralCatch.java
@@ -0,0 +1,16 @@
+/*
+WhitespaceAfter
+tokens = LITERAL_CATCH
+
+
+*/
+
+package com.puppycrawl.tools.checkstyle.checks.whitespace.whitespaceafter;
+
+public class InputWhitespaceAfterLiteralCatch {
+    public static void main(String[] args) {
+
+      try {} catch (Exception e){} // OK
+      try {} catch(Exception e){} // violation ''catch' is not followed by whitespace'
+    }
+}