Try-Catch
Syntax
try($eval, $catch)
Description
Executes a try block and handles any errors that occur using a catch function. The try function implements a try-catch behavior for TL-Script that enables robust error handling.
The function works with two parameters:
- Try block: The code block or expression to be executed that could cause an error
- Catch function: The fallback function that is executed in the event of an error
If the try block is executed successfully, its result is returned. If an error occurs, the catch function is executed. It always receives two arguments: the error
message and the value.
- message: For an
I18NExceptiontheResKey, otherwise the error message as text. - value: The value passed as the third argument of
throw().nullif the error did not originate from athrow()orthrow()was called without a value.
Parameters
| Name | Type | Type Description | Mandatory | Standard |
|---|---|---|---|---|
|
eval |
Code block / expression | The code block or expression that is to be executed. | yes | - |
| catch | Function | The fallback function that is executed when an error occurs. Receives two arguments: the error message and the value passed via throw() (value). |
no | null |
Return value
Type: Object
Returns the result of the try block if it was executed successfully. If an error occurs, the result of the catch function is returned. If no catch function is specified,
nullis returned in the event of an error.
Examples
try(
sendMail(
subject: $subject,
to: $emailAddr,
body: $emailBody
),
catch: mailError -> {
info("E-Mail-Zustellung an " + $emailAddr + " fehlgeschlagen. Grund: " + $mailError);
false
}
)
Output: Attempts to send an e-mail. If successful, the result of sendMail is returned. If an error occurs, false is returned and a user info with the attempted e-mail address.