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 TLScript 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 with specific parameters:
- I18NException: catch function receives (ResKey, original argument)
- Regular exception: catch function receives (error message, original argument)
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 the exception object as a parameter | 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.