Self Service Lab - Try Catch
The purpose of this document is to provide a working example and explanation of exception handling using Try and Catch blocks within Pliant.
In this guide, we are going to
A) Provide background on Try and Catch operations in a general sense,
B) Use an example workflow to demonstrate the behavior of Try
C) use Catch to fetch the error.
A. Introduction: Try & Catch
In programming, exception handling is the process by which exceptions or errors raised during the execution of a script are handled by the program. In the context of a Pliant workflow, we can handle potentially flow-breaking errors gracefully using the Try and Catch blocks. The Try and Catch blocks always occur in the following sequence:
We "try" to execute a series of blocks (in our case, a sequence of blocks), which we suspect may raise an error or exception, directly following our Try block.
If we do not encounter an error or exception in the Try operation, we skip all of the blocks contained under the Catch and resume normal workflow execution, having not encountered any errors. However, If an error or exception is encountered in the Try sequence, the workflow immediately skips the rest of the Try sequence and starts executing the Catch sequence of blocks. The data of the exception or error which we have "caught" in the try statement will be accessible within the Catch sequence of blocks as a variable, allowing for specific error handling. Once we've finished executing the catch sequence of blocks / we reach the End Catch block, the workflow resumes execution.
Pseudocode / Pliant Try Catch Sequence. Note how the Catch block has "Try_1" above it, signifying there is a variable called "Try_1" we can access inside the catch statement. Part III will demonstrate accessing the original error through this variable.
B. Exploring Try Behavior
B1. In Pliant, navigate to the Workflows tab in the main navigation
B2. Click Create, Create Flow.
B3. Title the workflow and click Create
B4. Click "Run" and execute the workflow.
Observe the result.
B5. Lets invoke a simple error in a workflow. We can use the Throw block, found in Common > Throw, to accomplish this. Navigate into the Common folder in the integrations menu on the right hand side of the screen and drag the Throw block into the workflow above the Assign block.
Specify the error message for the error we will throw within the Error Message field of the throw block.
B6. Click Run to execute the flow again and observe the output.
B7. Now, try enclosing the thrown error in a Try block. In the Common folder, find the Try block. Drag it into the workflow below the Assign block.
B8. Click and drag the Throw block to within the Try statement, just below the Try block.
B9. The resulting flow will resemble the following:
Click "Run" and execute the workflow.
Note that by placing the erroneous block within the Try statement, the workflow is able to complete its execution and we receive an output.
C. Example Use of Catch
Now that we can mitigate errors in execution with Try blocks, we can also do something useful with those errors.
C1. Drag an Assign block into the catch statement.
C2. The Catch block has two output parameters. Click the chevron to see them.
C3. Assign the output variable, $result, to Try_1.message as follows:
C4. Verify the resulting flow is resembles the following:
C5. Click Run and execute the workflow. The error which used to block our flow from executing is now simply returned as an output variable.
C6. Alternatively, we can Assign the $result variable to the value of Try_1.stack.
This returns the stack trace of the error instead of the error message (upon execution) in object form. Run the workflow and observe the result.