Class TryCatchStatementBuilder


  • public class TryCatchStatementBuilder
    extends Object
    This builder tries to make it easier to build a try/catch statement.

    AST
    TryCatchStatement stmt = A.STMT.tryCatchSBuilder()
            .tryStmt(protectedStmt)
            .addCatchStmt(Exception, 'ex', handlerOneStmt)
            .addCatchStmt(IllegalStatementException, 'ix', handlerTwoStmt)
            // ... add as many catch as you need
            .finallyStmt(finallyStatement)
            .build()
    Result
    try {
        // protectedStmt
       } catch(Exception ex) {
        // handlerOneStmt
       } catch(IllegalStateException ix) {
        // handlerTwoStmt
       } finally {
        // finallyStatement
       }
    Since:
    0.2.3
    • Constructor Detail

      • TryCatchStatementBuilder

        public TryCatchStatementBuilder()
    • Method Detail

      • tryStmt

        public TryCatchStatementBuilder tryStmt​(Statement stmt)
        Adds the try statement
        Parameters:
        stmt - the code you want to protect
        Returns:
        the current builder instance
        Since:
        0.2.3
      • addCatchStmt

        public TryCatchStatementBuilder addCatchStmt​(Class exceptionType,
                                                     String exceptionVarName,
                                                     Statement stmt)
        Adds one catch statement to catch a specific exception giving a specific name for the exception variable.

        IMPORTANT: This method uses Class to specify the class of the Exception. Don't use it if you are targetting a very early compilation stage. If so use the method using ClassNode
        Parameters:
        exceptionType - the Class of the exception you want to catch
        exceptionVarName - the name of the exception variable
        stmt - the code handling the exception
        Returns:
        the current builder instance
        Since:
        0.2.3
      • addCatchStmt

        public TryCatchStatementBuilder addCatchStmt​(ClassNode exceptionType,
                                                     String exceptionVarName,
                                                     Statement stmt)
        Adds one catch statement to catch a specific exception giving a specific name for the exception variable.
        Parameters:
        exceptionType - the ClassNode of the exception you want to catch
        exceptionVarName - the name of the exception variable
        stmt - the code handling the exception
        Returns:
        the current builder instance
        Since:
        0.2.3
      • finallyStmt

        public TryCatchStatementBuilder finallyStmt​(Statement stmt)
        Adds the statement that will be placed inside the finally block
        Parameters:
        stmt - the code executed in the finally block
        Returns:
        the current builder instance
        Since:
        0.2.3