public class TryCatchStatement
extends Statement
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.
| Constructor and description |
|---|
TryCatchStatement(Statement tryStatement, Statement finallyStatement)Constructs a TryCatchStatement with a try block and optional finally block. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public TryCatchStatement |
addCatch(CatchStatement catchStatement)Adds a CatchStatement} to this try-catch block and returns this for method chaining. |
|
public TryCatchStatement |
addResource(ExpressionStatement resourceStatement)Adds a resource ExpressionStatement} representing a try-with-resources declaration. |
|
public CatchStatement |
getCatchStatement(int idx)Returns the CatchStatement at the specified index. |
|
public List<CatchStatement> |
getCatchStatements()Returns the list of all CatchStatements in order of declaration. |
|
public Statement |
getFinallyStatement()Returns the finally block Statement, executed after the try and catch blocks complete. |
|
public ExpressionStatement |
getResourceStatement(int idx)Returns the resource ExpressionStatement at the specified index, representing a try-with-resources variable declaration. |
|
public List<ExpressionStatement> |
getResourceStatements()Returns the list of all resource ExpressionStatements declared in try-with-resources. |
|
public Statement |
getTryStatement()Returns the try block Statement, which is executed when the try-catch is entered. |
|
public static boolean |
isResource(Expression expression)Checks if an Expression is marked as a resource in a try-with-resources block. |
|
public void |
setCatchStatement(int idx, CatchStatement catchStatement)Replaces the CatchStatement} at the specified index. |
|
public void |
setFinallyStatement(Statement finallyStatement)Sets the finally block Statement}. |
|
public void |
setTryStatement(Statement tryStatement)Sets the try block Statement}. |
|
public void |
visit(GroovyCodeVisitor visitor) |
| Methods inherited from class | Name |
|---|---|
class Statement |
addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabel |
class ASTNode |
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit |
Constructs a TryCatchStatement with a try block and optional finally block. Catch statements and resource statements should be added separately via addCatch(CatchStatement) and addResource(ExpressionStatement).
Adds a CatchStatement} to this try-catch block and returns this for method chaining.
catchStatement
- the CatchStatement} to addAdds a resource ExpressionStatement} representing a try-with-resources declaration. The expression must be a DeclarationExpression or VariableExpression.
resourceStatement
- the resource ExpressionStatement} to addReturns the CatchStatement at the specified index.
idx
- the index of the catch statementReturns the list of all CatchStatements in order of declaration.
Returns the finally block Statement, executed after the try and catch blocks complete. May be null if no finally clause is present.
Returns the resource ExpressionStatement at the specified index, representing a try-with-resources variable declaration.
idx
- the index of the resource statementReturns the list of all resource ExpressionStatements declared in try-with-resources.
Returns the try block Statement, which is executed when the try-catch is entered.
Checks if an Expression is marked as a resource in a try-with-resources block.
expression
- the Expression to checkReplaces the CatchStatement} at the specified index.
idx
- the index of the catch statement to replacecatchStatement
- the new CatchStatement}Sets the finally block Statement}.
finallyStatement
- the finally block Statement}, or null for no finally clauseSets the try block Statement}.
tryStatement
- the try block Statement}Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.