Guidelines for error handling
You must be aware of the guidelines for error handling when writing a PowerShell or Perl script for OnCommand Workflow Automation (WFA).
PowerShell error handling
Guidelines | Example |
---|---|
Common parameters added to cmdlets by PowerShell runtime include error handling parameters such as ErrorAction and WarningAction:
For more information, you can use the |
ErrorAction: the following example shows how to handle a non-terminating error as a terminating error: New-NcIgroup-Name $IgroupName-Protocol $Protocol-Type$OSType-ErrorActionstop WarningAction New-VM-Name $VMName-VM $SourceVM-DataStore$DataStoreName-VMHost$VMHost-WarningActionSilentlyContinue |
Use the general “try/catch” statement if the type of the incoming exception is unknown. |
try { "In Try/catch block" } catch { "Got exception" } |
Use the specific “try/catch” statement if the type of the incoming exception is known. |
try { "In Try/catch block" } catch[System.Net.WebExceptional], [System.IO. IOException] { "Got exception" } |
Use the “finally” statement to release resources. |
try { "In Try/catch block" } catch { "Got exception" } finally { "Release resources" } |
Use PowerShell automatic variables to access information about exceptions. |
try { Get-WFALogger -Info -message $("Creating Ipspace: " + $Ipspace) New-NaNetIpspace-Name $Ipspace } catch { Throw "Failed to create Ipspace. Message: " + $_.Exception.Message; } |
Perl error handling
Guidelines | Example |
---|---|
Perl does not include native language support for try/catch blocks. Use eval blocks for checking and handling errors. Keep eval blocks as small as possible. |
eval { $wfa_util->sendLog('INFO', "Quiescing the relationship : $DestinationCluster://$DestinationVserver /$DestinationVolume" ); $server->snapmirror_quiesce( 'destination-vserver' => $DestinationVserver, 'destination-volume' => $DestinationVolume ); $wfa_util->sendLog('INFO', 'Quiesce operation started successfully.'); }; $wfa_util->checkEvalFailure( "Failed to quiesce the SnapMirror relationship $DestinationCluster://$DestinationVserver /$DestinationVolume", $@ ); |