Since Checkstyle 6.14
catch
parameter names conform to a specified pattern.
Default pattern has the following characteristic:
e
abbreviation (suitable for exceptions end errors)ex
abbreviation (suitable for exceptions)t
abbreviation (suitable for throwables)_
for unnamed catch parameterse1
or t2
pException
ie
or ee
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 |
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' } } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.naming