Since Checkstyle 6.14
Checks that 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)e1
or t2
pException
ie
or ee
name | description | type | default value | since |
---|---|---|---|---|
format | Specifies 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 MyTest { public void myTest() { try { // ... } catch (ArithmeticException e) { // OK // ... } catch (ArrayIndexOutOfBoundsException ex) { // OK // ... } catch (Throwable t) { // OK // ... } catch (IndexOutOfBoundsException e123) { // violation, digits // not allowed // ... } catch (NullPointerException ab) { // violation, should have at least // three characters if not e|t|ex // ... } catch (ArrayStoreException abc) { // OK // ... } catch (InterruptedException aBC) { // violation, first two characters // should be in lowercase // ... } catch (RuntimeException abC) { // OK // ... } catch (Exception abCD) { // OK // ... } } }
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 MyTest { public void myTest() { try { // ... } catch (ArithmeticException ex) { // OK // ... } catch (ArrayIndexOutOfBoundsException ex2) { // OK // ... } catch (IOException thirdException) { // OK // ... } catch (Exception FourthException) { // violation, the initial letter // should be lowercase // ... } } }
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