Since Checkstyle 3.2
name | description | type | default value | since |
---|---|---|---|---|
max | Specify maximum allowed nesting depth. | int | 1 |
3.2 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="NestedTryDepth"/> </module> </module>
Example1:
public class Example1 { void testMethod() { try { try { // OK, current depth is 1, default max allowed depth is also 1 } catch (Exception e) {} } catch (Exception e) {} try { try { try { // violation, current depth is 2, default max allowed depth is 1 } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} try { try { try { // violation, current depth is 2, default max allowed depth is 1 try { // violation, current depth is 3, default max allowed depth is 1 try { // violation, current depth is 4, default max allowed depth is 1 } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } }
To configure the check to allow nesting depth 3:
<module name="Checker"> <module name="TreeWalker"> <module name="NestedTryDepth"> <property name="max" value="3"/> </module> </module> </module>
Example2:
public class Example2 { void testMethod() { try { try { // OK, current depth is 1, max allowed depth is 3 } catch (Exception e) {} } catch (Exception e) {} try { try { try { // OK, current depth is 2, max allowed depth is also 3 } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} try { try { try { // OK, current depth is 2, max allowed depth is 3 try { // OK, current depth is 3, max allowed depth is 3 try { // violation, current depth is 4, max allowed depth is 3 } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } catch (Exception e) {} } }
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.coding