Try / Catch
Try / Catch
Section titled “Try / Catch”Type: try-catch · Package: Core Activities v1.0.0
Execute a block of activities and, if any fail, instead of stopping the entire execution, jump to an alternative block where you can react: log the error, notify, or follow a plan B. It is the key tool for your automation to be robust in the face of problems that you know may occur (a page that does not load, a file that does not exist, a system that sometimes crashes).
It has three blocks: try (what you try), catch (what to do if it fails), and finally (what always runs, no matter what).
How it works
Section titled “How it works”- Executes the activities of
try. - If all are successful, omit the
catch. - If anything fails, stop
tryat that point, save the error toerrorVariable(if you defined it) —aZoanErrorobject with the exception details— and run thecatchblock. - In the end, it always executes
finally—whether there was an error or not— ideal for freeing up resources (closing a browser, a connection).
After catch, execution continues normally with whatever comes after the Try/Catch: the error was “trapped”.
Optional parameters
Section titled “Optional parameters”| Parameter | Editor | Description |
|---|---|---|
errorVariable | variable name | Variable of type ZoanError holding the captured error. In the catch, access its properties: .Message (the message), .Type (the exception type, e.g. "HttpRequestException"), .Source and .StackTrace. |
Subflows (lanes)
Section titled “Subflows (lanes)”try requerido
Section titled “try requerido”Activities to try. If one fails, the rest of the try does not run.
catch optional
Section titled “catch optional”Activities that run only if something on the try failed.
finally optional
Section titled “finally optional”Activities that run always, whether try has failed or not.
Example
Section titled “Example”Trying to read an Excel; if it fails, log the error and continue; and close the book no matter what:
Try / Catch errorVariable = error ├─ try: │ Excel Open path = = asset("datos.xlsx") output = libro │ Excel Read Range excel = = libro range = "A1:D100" output = tabla ├─ catch: │ Log level = error message = = "Could not read the Excel (" + error.Type + "): " + error.Message │ Set Variable name = tabla value = = newDataTable() // default value └─ finally: Excel Close excel = = libro