Try / Catch
Try / Catch
Section titled “Try / Catch”Type: try-catch · Package: Core Activities v1.0.8
Runs a block of activities and, if any of them fails, instead of stopping the whole execution, jumps to an alternative block where you can react: log the error, notify, or follow a plan B. It is the key tool to make your automation robust against the problems you know can happen (a page that doesn’t load, a file that doesn’t exist, a system that sometimes fails).
It has three blocks: try (what you attempt), catch (what to do if it fails) and finally (what always runs, no matter what).
How it works
Section titled “How it works”- Runs the
tryactivities. - If they all succeed, it skips the
catch. - If any fails, it stops the
tryat that point, stores the error inerrorVariable(if you set one) —aZoanErrorobject with the details of the exception— and runs thecatchblock. - At the end, it always runs
finally—whether there was an error or not— ideal for releasing resources (closing a browser, a connection).
After the catch, execution continues normally with whatever comes after the Try/Catch: the error was “caught”. Details worth knowing:
- An empty
catchalso catches the error: the flow goes on as if nothing happened. Use it only if you really want to ignore the failure, and leave at least a Log. - An error inside the
catchor thefinallyis not caught by this same Try/Catch: it goes up to the next one that contains it, or makes the execution fail. - To throw the error again after logging it, use Throw with
message = error.MessageanderrorType = error.Type.
Required parameters
Section titled “Required parameters”None. (The only mandatory block is the try sub-flow.)
Optional parameters
Section titled “Optional parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
errorVariable | string | — | Name of the variable that receives the caught error, of type ZoanError. In the catch, access .Message, .Type, .Source and .StackTrace. You don’t need to declare it; if you declared it as text (string), it receives only the message. |
Sub-flows
Section titled “Sub-flows”try required
Section titled “try required”Activities to attempt. If one fails, the rest of the try does not run.
catch optional
Section titled “catch optional”Activities that run only if something in the try failed.
finally optional
Section titled “finally optional”Activities that always run, whether the try failed or not. Also if someone terminates the execution (Terminate in Zoan Cloud, the Designer or the Agent): the finally has 10 seconds to close what is open, so it is the place to close the application or the browser. If it doesn’t finish in that time, a warning is left in the log and the execution ends anyway.
Output
Section titled “Output”This activity does not return a value. The caught error is stored in the errorVariable variable:
| Property | Content |
|---|---|
.Message | The error message. |
.Type | The type: the errorType of a Throw (PlaybookException by default, BusinessRuleException for business errors) or the name of the system exception (TimeoutException, HttpRequestException…). |
.Source | The component where it originated. |
.StackTrace | The trace, for diagnosis. |
If the error comes from an invoked playbook or process, ZoanError shows the original error, not the invocation’s wrapper.
Example
Section titled “Example”Try to read an Excel file; if it fails, log the error and continue; and close the workbook no matter what:
Try / Catch errorVariable = error ├─ try: │ Excel Open path = asset("data.xlsx") → output: workbook │ Excel Read Range excel = workbook range = "A1:D100" → output: table ├─ catch: │ Log level = error message = "Could not read the Excel file (" + error.Type + "): " + error.Message │ Set Variable name = table value = newDataTable() // default value └─ finally: Excel Close excel = workbookCommon errors
Section titled “Common errors”| Message | Cause and fix |
|---|---|
Finally did not complete after the execution was terminated: … | Warning: the execution was terminated and the finally did not finish within 10 seconds (or failed). Keep only quick cleanup in the finally. |
| The error gets “lost” | The catch is empty and there is no errorVariable: the failure is caught without a trace. Log the error with a Log or throw it again with Throw. |