Class 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 CatchStatements for exception handlers, an optional finally block, and optional resource declarations that implement AutoCloseable.
See Also:
  • 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 CatchStatements in order of declaration.
      Returns:
      a List of CatchStatements
    • 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 ExpressionStatements declared in try-with-resources.
      Returns:
      a List of resource ExpressionStatements
    • 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