Package org.codehaus.groovy.ast.stmt
Class TryCatchStatement
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.stmt.Statement
org.codehaus.groovy.ast.stmt.TryCatchStatement
- All Implemented Interfaces:
NodeMetaDataHandler
Represents a try { ... } catch { ... } finally { ... } statement in Groovy.
A try-catch statement combines exception handling with optional resource management (try-with-resources).
It contains a main try block, zero or more
CatchStatements for exception handlers,
an optional finally block, and optional resource declarations that implement AutoCloseable.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTryCatchStatement(Statement tryStatement, Statement finallyStatement) Constructs a TryCatchStatement with a try block and optional finally block. -
Method Summary
Modifier and TypeMethodDescriptionaddCatch(CatchStatement catchStatement) Adds aCatchStatement} to this try-catch block and returns this for method chaining.addResource(ExpressionStatement resourceStatement) Adds a resourceExpressionStatement} representing a try-with-resources declaration.getCatchStatement(int idx) Returns theCatchStatementat the specified index.Returns the list of allCatchStatements in order of declaration.Returns the finally blockStatement, executed after the try and catch blocks complete.getResourceStatement(int idx) Returns the resourceExpressionStatementat the specified index, representing a try-with-resources variable declaration.Returns the list of all resourceExpressionStatements declared in try-with-resources.Returns the try blockStatement, which is executed when the try-catch is entered.static booleanisResource(Expression expression) Checks if anExpressionis marked as a resource in a try-with-resources block.voidsetCatchStatement(int idx, CatchStatement catchStatement) Replaces theCatchStatement} at the specified index.voidsetFinallyStatement(Statement finallyStatement) Sets the finally blockStatement}.voidsetTryStatement(Statement tryStatement) Sets the try blockStatement}.voidvisit(GroovyCodeVisitor visitor) Accepts a code visitor for AST traversal and transformation.Methods inherited from class org.codehaus.groovy.ast.stmt.Statement
addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabelMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Constructor Details
-
TryCatchStatement
Constructs a TryCatchStatement with a try block and optional finally block. Catch statements and resource statements should be added separately viaaddCatch(CatchStatement)andaddResource(ExpressionStatement).
-
-
Method Details
-
visit
Description copied from class:ASTNodeAccepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.- Overrides:
visitin classASTNode- Parameters:
visitor- theGroovyCodeVisitorto process this node
-
getTryStatement
Returns the try blockStatement, which is executed when the try-catch is entered.- Returns:
- the try block
Statement}
-
getFinallyStatement
Returns the finally blockStatement, executed after the try and catch blocks complete. May be null if no finally clause is present.- Returns:
- the finally block
Statement}, or null
-
getCatchStatement
Returns theCatchStatementat the specified index.- Parameters:
idx- the index of the catch statement- Returns:
- the
CatchStatementat the index, or null if the index is out of bounds
-
getCatchStatements
Returns the list of allCatchStatements in order of declaration.- Returns:
- a
ListofCatchStatements
-
getResourceStatement
Returns the resourceExpressionStatementat the specified index, representing a try-with-resources variable declaration.- Parameters:
idx- the index of the resource statement- Returns:
- the resource
ExpressionStatement} at the index, or null if the index is out of bounds
-
getResourceStatements
Returns the list of all resourceExpressionStatements declared in try-with-resources.- Returns:
- a
Listof resourceExpressionStatements
-
isResource
Checks if anExpressionis marked as a resource in a try-with-resources block.- Parameters:
expression- theExpressionto check- Returns:
- true if the expression is a resource declaration, false otherwise
-
setTryStatement
Sets the try blockStatement}.- Parameters:
tryStatement- the try blockStatement}
-
setFinallyStatement
Sets the finally blockStatement}.- Parameters:
finallyStatement- the finally blockStatement}, or null for no finally clause
-
setCatchStatement
Replaces theCatchStatement} at the specified index.- Parameters:
idx- the index of the catch statement to replacecatchStatement- the newCatchStatement}
-
addCatch
Adds aCatchStatement} to this try-catch block and returns this for method chaining.- Parameters:
catchStatement- theCatchStatement} to add- Returns:
- this
TryCatchStatement} for method chaining
-
addResource
Adds a resourceExpressionStatement} representing a try-with-resources declaration. The expression must be aDeclarationExpressionorVariableExpression.- Parameters:
resourceStatement- the resourceExpressionStatement} to add- Returns:
- this
TryCatchStatement} for method chaining - Throws:
GroovyBugError- if the resource expression is not a DeclarationExpression or VariableExpression
-