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`

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 `CatchStatement`s for exception handlers, an optional finally block, and optional resource declarations that implement AutoCloseable.

See Also:
  - `Statement`
  - `CatchStatement`
  - `ExpressionStatement`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TryCatchStatement(Statement tryStatement, Statement finallyStatement)`
  Constructs a TryCatchStatement with a try block and optional finally block.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `TryCatchStatement`
  `addCatch(CatchStatement catchStatement)`
  Adds a `CatchStatement`} to this try-catch block and returns this for method chaining.
  `TryCatchStatement`
  `addResource(ExpressionStatement resourceStatement)`
  Adds a resource `ExpressionStatement`} representing a try-with-resources declaration.
  `CatchStatement`
  `getCatchStatement(int idx)`
  Returns the `CatchStatement` at the specified index.
  `List<CatchStatement>`
  `getCatchStatements()`
  Returns the list of all `CatchStatement`s in order of declaration.
  `Statement`
  `getFinallyStatement()`
  Returns the finally block `Statement`, executed after the try and catch blocks complete.
  `ExpressionStatement`
  `getResourceStatement(int idx)`
  Returns the resource `ExpressionStatement` at the specified index, representing a try-with-resources variable declaration.
  `List<ExpressionStatement>`
  `getResourceStatements()`
  Returns the list of all resource `ExpressionStatement`s declared in try-with-resources.
  `Statement`
  `getTryStatement()`
  Returns the try block `Statement`, which is executed when the try-catch is entered.
  `static boolean`
  `isResource(Expression expression)`
  Checks if an `Expression` is marked as a resource in a try-with-resources block.
  `void`
  `setCatchStatement(int idx, CatchStatement catchStatement)`
  Replaces the `CatchStatement`} at the specified index.
  `void`
  `setFinallyStatement(Statement finallyStatement)`
  Sets the finally block `Statement`}.
  `void`
  `setTryStatement(Statement tryStatement)`
  Sets the try block `Statement`}.
  `void`
  `visit(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, setStatementLabel`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## Constructor Details

  - ### TryCatchStatement

    public TryCatchStatement(Statement tryStatement, Statement finallyStatement)
    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)`.
    Parameters:
      `tryStatement` - the `Statement` to execute as the try block
      `finallyStatement` - the `Statement` to execute in the finally block, or null if no finally clause

- ## Method Details

  - ### visit

    public void visit(GroovyCodeVisitor visitor)
    Description copied from class: `ASTNode`
    Accepts 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:
      `visit` in class `ASTNode`
    Parameters:
      `visitor` - the `GroovyCodeVisitor` to process this node

  - ### getTryStatement

    public Statement getTryStatement()
    Returns the try block `Statement`, which is executed when the try-catch is entered.
    Returns:
      the try block `Statement`}

  - ### getFinallyStatement

    public Statement getFinallyStatement()
    Returns the finally block `Statement`, 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

    public CatchStatement getCatchStatement(int idx)
    Returns the `CatchStatement` at the specified index.
    Parameters:
      `idx` - the index of the catch statement
    Returns:
      the `CatchStatement` at the index, or null if the index is out of bounds

  - ### getCatchStatements

    public List<CatchStatement> getCatchStatements()
    Returns the list of all `CatchStatement`s in order of declaration.
    Returns:
      a `List` of `CatchStatement`s

  - ### getResourceStatement

    public ExpressionStatement getResourceStatement(int idx)
    Returns the resource `ExpressionStatement` at 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

    public List<ExpressionStatement> getResourceStatements()
    Returns the list of all resource `ExpressionStatement`s declared in try-with-resources.
    Returns:
      a `List` of resource `ExpressionStatement`s

  - ### isResource

    public static boolean isResource(Expression expression)
    Checks if an `Expression` is marked as a resource in a try-with-resources block.
    Parameters:
      `expression` - the `Expression` to check
    Returns:
      true if the expression is a resource declaration, false otherwise

  - ### setTryStatement

    public void setTryStatement(Statement tryStatement)
    Sets the try block `Statement`}.
    Parameters:
      `tryStatement` - the try block `Statement`}

  - ### setFinallyStatement

    public void setFinallyStatement(Statement finallyStatement)
    Sets the finally block `Statement`}.
    Parameters:
      `finallyStatement` - the finally block `Statement`}, or null for no finally clause

  - ### setCatchStatement

    public void setCatchStatement(int idx, CatchStatement catchStatement)
    Replaces the `CatchStatement`} at the specified index.
    Parameters:
      `idx` - the index of the catch statement to replace
      `catchStatement` - the new `CatchStatement`}

  - ### addCatch

    public TryCatchStatement addCatch(CatchStatement catchStatement)
    Adds a `CatchStatement`} to this try-catch block and returns this for method chaining.
    Parameters:
      `catchStatement` - the `CatchStatement`} to add
    Returns:
      this `TryCatchStatement`} for method chaining

  - ### addResource

    public TryCatchStatement addResource(ExpressionStatement resourceStatement)
    Adds a resource `ExpressionStatement`} representing a try-with-resources declaration. The expression must be a `DeclarationExpression` or `VariableExpression`.
    Parameters:
      `resourceStatement` - the resource `ExpressionStatement`} to add
    Returns:
      this `TryCatchStatement`} for method chaining
    Throws:
      `GroovyBugError` - if the resource expression is not a DeclarationExpression or VariableExpression