Skip to content

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).

  1. Executes the activities of try.
  2. If all are successful, omit the catch.
  3. If anything fails, stop try at that point, save the error to errorVariable (if you defined it) —a ZoanError object with the exception details— and run the catch block.
  4. 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”.

ParameterEditorDescription
errorVariablevariable nameVariable 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.

Activities to try. If one fails, the rest of the try does not run.

Activities that run only if something on the try failed.

Activities that run always, whether try has failed or not.

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
  • Retry — automatically retry a failing block.
  • Throw — throw an error on purpose (which an external Try/Catch can catch).
  • Log — log the captured error to catch.