Handling errors and exceptions
Every workflow that touches a real system will eventually meet an error: a VM that isn’t there, a vCenter that times out, a name that matches twice. What matters is what happens next. Without error handling, the run stops at the step that failed, its state becomes Failed, and the message is whatever the failing code happened to say. With it, you choose: log something useful, clean up, try another way, or fail on purpose with a message the person running the workflow can act on.
This recipe builds Find a VM by name, which turns a typed VM name into exactly one VC:VirtualMachine, and fails clearly when it can’t.
Getting ready
- The action
com.vcfguide.chapter3/findVmsByNamefrom recipe 3.2. - The folder VCF Orchestrator Guide/Chapter 3.
- The name of a VM that exists in the lab, and a name that doesn’t.
How to do it
Build the happy path
- Create a workflow called
Find a VM by namein VCF Orchestrator Guide/Chapter 3. Give it an inputvmNameof typestringand an outputvmof typeVC:VirtualMachine. - On the Variables tab, add a variable
errorMessageof typestring. - On the Schema tab, add a Scriptable task called
Find the VM, withvmNamein andvmout, and this script:
// Match the whole name, so "web" doesn't also find "web01" and "web02"var exactName = "^" + vmName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$";var matches = System.getModule("com.vcfguide.chapter3").findVmsByName(exactName);
if (matches.length === 0) { throw "No VM is called " + vmName;}if (matches.length > 1) { throw matches.length + " VMs are called " + vmName + ". Rename one, or look it up in a single vCenter.";}vm = matches[0];System.log("Found " + vm.name + " (" + vm.id + ")");- Save and run it twice: once with a VM name that exists, once with one that doesn’t. The second run ends in the Failed state, and the run’s error is the text you threw. You haven’t handled anything yet. That’s the default behaviour.
Catch the error on an exception path
- Select
Find the VM. On its General tab, under Exception handling, click Select variable and chooseerrorMessage. From now on, an error in this element is caught, and its message is copied intoerrorMessage. - Drag a new Scriptable task from the palette and drop it onto
Find the VM. A red dashed arrow appears between them: that’s the element’s exception path. Name the new taskExplain the failure, and bindvmNameanderrorMessageas inputs anderrorMessageas an output:
System.error("Lookup of '" + vmName + "' failed: " + errorMessage);errorMessage = "Could not find the VM '" + vmName + "'. " + errorMessage + ". Check the spelling, or ask the vSphere team whether it was renamed.";- Drag a Thrown exception element from the Generic group onto the arrow after
Explain the failure. Bind its input toerrorMessage.

- Save and run it again with the missing name. The run still fails, which is what you want, but its error now says what went wrong and what to do about it, and the log has an error-level line from
Explain the failure.

Look closely at the message in figure 3.11. When Orchestrator catches an error and copies it into errorMessage, it appends where the error came from: (Workflow:Find a VM by name / Find the VM (item1)#5), that is, the workflow, the element, its item ID, and the script line. That helps in a log, but it’s clumsy in a message for a person. To keep only your own text, strip it in Explain the failure, for example with errorMessage = errorMessage.replace(/ \(Workflow:.*\)$/, "");.
Catch everything else with a default error handler
- Drag a Default error handler from the Generic group onto an empty part of the canvas. It isn’t linked to any element. It catches any error that an element’s own exception path doesn’t.
- Drop a Scriptable task called
Log unexpected erroronto it and link that to a second Thrown exception, bound toerrorMessageas in step 7. Its script can be one line:
System.error("Unexpected failure in Find a VM by name: " + errorMessage);- Save. To test it, add
var broken = vmName.noSuchFunction();as the first line of a new scriptable task in front ofFind the VM, run the workflow, and check that the run goes toLog unexpected error. Then remove the test task.

How it works
Every element except the start, end, and decision elements has two ways out: the normal path and the exception path. When the element’s script throws, or a plug-in call inside it fails, Orchestrator looks for somewhere to send the error:
- If the element has an exception path, Orchestrator copies the error’s message into the variable you chose under Exception handling, then follows the red dashed arrow.
- If not, and the workflow has a default error handler, the run goes there.
- If there’s neither, the run stops and ends in the Failed state, with the error as its result.
That’s the same idea as try and catch in a script, applied to a whole element: the element is the try block, and the exception path is the catch.
The Thrown exception element does the opposite. It ends the run on purpose, in the Failed state, with the string you bind to it as the error. Ending on an exception rather than on a normal end element matters to anything that runs the workflow. A parent workflow sees the child fail and can follow its own exception path. A VCF Automation extensibility subscription that blocks on the workflow sees the failure and stops the deployment. A REST client sees the run’s state as failed. If you caught the error and then ended normally, they’d all think it worked.
throw in an Orchestrator script accepts a plain string, and that string becomes the error message. You can also throw an Error object, throw new Error("…"), and the message is read from it.
new Error(“text”) instead of a string, for example whether it starts with Error: or includes a line number.errorCode attribute, then drag from the element's red connector to draw the exception link in the Java client.string variable will do, though many teams still call it errorCode.There's more
Use try and catch for errors you expect to recover from. An exception path is right when the error changes what the workflow does next. When the step can carry on by itself, catch the error in the script and keep the schema simple:
var hostName = "unknown";try { hostName = vm.runtime.host.name;} catch (e) { // The VM may be mid-migration, or the host may be disconnected System.warn("Could not read the host of " + vm.name + ": " + e);}System.log(vm.name + " runs on " + hostName);Don’t swallow errors. A catch that only logs, or an exception path that leads to a normal end element, turns a failure into a success. Do it only when the workflow really has done its job. If not, finish the exception path with a Thrown exception.
Fail the loop from recipe 3.3. In Wait for VMware Tools, replace the end element on the false path of Try again? with a Thrown exception bound to result. When VMware Tools never starts, the run fails, and the error says what Tools last reported.
Keep going in a foreach. To process the remaining items when one fails, handle the error inside the sub-workflow. Give its risky step an exception path that logs the error, sets an output such as status to failed, and ends normally. The foreach element then carries on, and its collected outputs tell you which items failed.
Errors in actions. An action element behaves like any other element: it gets an exception path and the Exception handling setting. When you call an action from a script with System.getModule(...), its errors are thrown into your script, so wrap the call in try and catch if you want to handle them there.
Clean up on the way out. If a workflow creates something before it fails, such as a snapshot or a temporary VM, the default error handler is a good place to remove it. Keep cleanup steps simple and give them their own exception path, so a failed cleanup doesn’t hide the original error.
Resume instead of restart. A failed run can be resumed from the step that failed, once you’ve fixed the cause. Recipe 3.5 covers resuming and debugging failed runs.
See also
- 3.3 Decisions, switches, and loops, for the loop this recipe makes fail.
- Broadcom TechDocs (VCF 9): Exception Handling, Logical Flow of a Workflow, and Define User Interaction Exception Behavior.
- VMware Docs (vRealize Orchestrator 8.x): Create exception bindings and Define user interaction exception behavior.
