public final class NestedTryDepthCheck extends AbstractCheck
Restricts nested try-catch-finally blocks to a specified depth.
max
- Specify maximum allowed nesting depth.
Type is int
.
Default value is 1
.
To configure the check:
<module name="NestedTryDepth"/>
case 1: Example of code with violation:
try { try { try { // violation, current depth is 2, default max allowed depth is 1 } catch (Exception e) { } } catch (Exception e) { } } catch (Exception e) { }
case 1: Example of compliant code:
try { try { // OK, current depth is 1, default max allowed depth is also 1 } catch (Exception e) { } } catch (Exception e) { }
case 2: Example of code for handling unique and general exceptions
try { try { // OK, current depth is 1, default max allowed depth is also 1 // any more nesting could cause code violation! throw ArithmeticException(); } catch (ArithmeticException e) { // catches arithmetic exceptions } catch (NumberFormatException e) { // catches number-format exceptions } catch (Exception e) { // catches general exceptions other than stated above } } catch ( ArithmeticException | NumberFormatException | ArrayIndexOutOfBoundsException e) { // catches any of the 3 exception } catch (Exception e) { // catches general exception } finally { // do something when try-catch block finished execution }
To configure the check to allow nesting depth 3:
<module name="NestedTryDepth"> <property name="max" value="3"/> </module>
Example of code with violation:
try { try { try { try { 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) { }
Example of compliant code:
try { try { try { try { // OK, current depth is 3, max allowed depth is also 3 } catch (Exception e) { } } catch (Exception e) { } } catch (Exception e) { } } catch (Exception e) { }
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
nested.try.depth
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
NestedTryDepthCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
leaveToken(DetailAST literalTry)
Called after all the child nodes have been process.
|
void |
setMax(int max)
Setter to specify maximum allowed nesting depth.
|
void |
visitToken(DetailAST literalTry)
Called to process a token.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public NestedTryDepthCheck()
public void setMax(int max)
max
- maximum allowed nesting depth.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST literalTry)
AbstractCheck
visitToken
in class AbstractCheck
literalTry
- the token to processpublic void leaveToken(DetailAST literalTry)
AbstractCheck
leaveToken
in class AbstractCheck
literalTry
- the token leavingCopyright © 2001–2020. All rights reserved.