CatchParameterName

Since Checkstyle 6.14

Description

Checks that catch parameter names conform to a specified pattern.

Default pattern has the following characteristic:

  • allows names beginning with two lowercase letters followed by at least one uppercase or lowercase letter
  • allows e abbreviation (suitable for exceptions end errors)
  • allows ex abbreviation (suitable for exceptions)
  • allows t abbreviation (suitable for throwables)
  • allows _ for unnamed catch parameters
  • prohibits numbered abbreviations like e1 or t2
  • prohibits one letter prefixes like pException
  • prohibits two letter abbreviations like ie or ee
  • prohibits any other characters than letters

Properties

name description type default value since
format Sets the pattern to match valid identifiers. Pattern "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$" 6.14

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="CatchParameterName"/>
  </module>
</module>
        

Example:

public class Example1 {
  public void myTest() {
    try {
      throw new InterruptedException();
    } catch (ArithmeticException e) {
    } catch (ArrayIndexOutOfBoundsException ex) {
    } catch (IndexOutOfBoundsException e123) {
      // violation above, 'Name 'e123' must match pattern'
    } catch (NullPointerException ab) {
      // violation above, 'Name 'ab' must match pattern'
    } catch (ArrayStoreException abc) {
    } catch (InterruptedException aBC) {
      // violation above, 'Name 'aBC' must match pattern'
    } catch (RuntimeException abC) {
    } catch (Exception EighthException) {
      // violation above, 'Name 'EighthException' must match pattern'
    } catch (Throwable t) {
    }
  }
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by any letters or digits is:

Configuration:

<module name="Checker">
  <module name="TreeWalker">
    <module name="CatchParameterName">
      <property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {
  public void myTest() {
    try {
      throw new InterruptedException();
    } catch (ArithmeticException e) {
      // violation above, 'Name 'e' must match pattern'
    } catch (ArrayIndexOutOfBoundsException ex) {
    } catch (IndexOutOfBoundsException e123) {
    } catch (NullPointerException ab) {
    } catch (ArrayStoreException abc) {
    } catch (InterruptedException aBC) {
    } catch (RuntimeException abC) {
    } catch (Exception EighthException) {
      // violation above, 'Name 'EighthException' must match pattern'
    } catch (Throwable t) {
      // violation above, 'Name 't' must match pattern'
    }
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker