Version

    Try-Catch Statement

    Since CloverDX 5.6, you may use the try-catch statement to handle runtime errors.

    For every try statement, there can be only one catch block as there is only one type of exception: CTLException. Additionally, there is no finally block. In CTL, there are two parts to try-catch statement:

    • try block allows you to define a code to be tested for errors

    • catch block’s purpose is to handle errors that might occur within the try block

    Depending on whether the try block encounters an error or not, the execution of the statement may lead to two alternatives:

    • If the try block executes without errors, the following catch block is skipped, and the whole try-catch statement completes successfully.

    • If the code inside the try block is erroneous, the execution jumps to the beginning of the respective catch block and executes the code within it.

    The implementation of how to handle the error is up to you. For example, you can throw a custom error using the raiseError() function, log the exception and have the execution finish successfully, or execute a custom code as a workaround for the exception. You can access some details about the exceptions via the CTLException data structure.

    try-catch statements can be nested.

    Example 86. Try-Catch Statement
    integer a = 123;
    integer b = 0;
    integer c;
    
    try {
        c = a / b; // throws ArithmeticException
        printLog(info, c); // skipped
    } catch (CTLException ex) {
        c = -1; // workaround: set the variable to -1 to indicate error
        printLog(warn, ex); // log a warning
    }

    CTLException is actually a data record with the following fields:

    sourceRow: integer

    the row of the CTL source code where the exception occurred

    sourceColumn: integer

    the column of the CTL source code where the exception occurred

    message: string

    the error message of the innermost exception - the original cause of the failure

    cause: string

    the type of the innermost exception, e.g. java.lang.ArithmeticException

    stackTrace: list[string]

    the cascade of function calls that caused the failure

    exceptionTrace: list[string]

    the list of exception types from the outermost to the innermost