Version

    Try-Catch Statement

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

    The execution starts by first executing the try block. Then there is a choice:

    • If the try block completes normally, then no further action is taken, the catch block is skipped and the whole try-catch statement completes normally.
    • If the code inside the try block throws an exception, the execution jumps to the beginning of the respective catch block. It is up to you how you handle the exception there. For example, you can execute some alternative code to work around the problem that caused the exception. Or you can throw a custom error using the raiseError() function. Or you can just log the exception and have the execution complete normally. Some details about the exception can be accessed via the CTLException data structure.

    For every try block, there can be only one catch block, because there is just one type of exception: CTLException. Also, there is no finally block in CTL.

    try-catch statements can be nested.

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