Package: org.codehaus.groovy.ast.stmt

## [Java] Class TryCatchStatement

- org.codehaus.groovy.ast.stmt.TryCatchStatement

-     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.

  See Also:
    Statement
    CatchStatement
    ExpressionStatement

- - ### Constructor Summary

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

  - ### Methods Summary

    Methods
    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)`

  - ### Inherited Methods Summary

    Inherited Methods
    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`

- - ### Constructor Detail

    - #### 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 Detail

    - #### 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

    - #### public TryCatchStatement addResource(ExpressionStatement resourceStatement)

      Adds a resource ExpressionStatement} representing a try-with-resources declaration. The expression must be a DeclarationExpression or VariableExpression.

      throws:
        GroovyBugError if the resource expression is not a DeclarationExpression or VariableExpression
      Parameters:
        `resourceStatement` - the resource ExpressionStatement} to add
      Returns:
        this TryCatchStatement} for method chaining

    - #### 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

    - #### public List<CatchStatement> getCatchStatements()

      Returns the list of all CatchStatements in order of declaration.

      Returns:
        a List of CatchStatements

    - #### 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

    - #### 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

    - #### public List<ExpressionStatement> getResourceStatements()

      Returns the list of all resource ExpressionStatements declared in try-with-resources.

      Returns:
        a List of resource ExpressionStatements

    - #### public Statement getTryStatement()

      Returns the try block Statement, which is executed when the try-catch is entered.

      Returns:
        the try block Statement}

    - #### 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

    - #### 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}

    - #### public void setFinallyStatement(Statement finallyStatement)

      Sets the finally block Statement}.

      Parameters:
        `finallyStatement` - the finally block Statement}, or null for no finally clause

    - #### public void setTryStatement(Statement tryStatement)

      Sets the try block Statement}.

      Parameters:
        `tryStatement` - the try block Statement}

    - #### public void visit(GroovyCodeVisitor visitor)