简体中文版经机器翻译而成,仅供参考。如与英语版出现任何冲突,应以英语版为准。 
        
        
      错误处理准则
您必须了解为 OnCommand Workflow Automation ( WFA )编写 PowerShell 或 Perl 脚本时的错误处理准则。
PowerShell 错误处理
| 准则 | 示例 | 
|---|---|
PowerShell 运行时添加到 cmdlet 的常见参数包括错误处理参数,例如 ErrorAction 和 WarningAction : 
 有关详细信息,您可以在 PowerShell CLI 中使用   | 
ErrorAction :以下示例显示了如何将非终止错误作为终止错误进行处理: New-NcIgroup-Name $IgroupName-Protocol $Protocol-Type$OSType-ErrorActionstop WarningAction New-VM-Name $VMName-VM $SourceVM-DataStore$DataStoreName-VMHost$VMHost-WarningActionSilentlyContinue  | 
如果传入异常的类型未知,请使用常规 "`try/catch` " 语句。  | 
try
{
"In Try/catch block"
}
catch
{
"Got exception"
}
 | 
如果已知传入异常的类型,请使用特定的 "`try/catch` " 语句。  | 
try
{
"In Try/catch block"
}
catch[System.Net.WebExceptional], [System.IO.
IOException]
{
"Got exception"
}
 | 
使用 "`finally` " 语句释放资源。  | 
try
{
"In Try/catch block"
}
catch
{
"Got exception"
}
finally
{
"Release resources"
}
 | 
使用 PowerShell 自动变量访问有关异常的信息。  | 
try
{
Get-WFALogger -Info -message $("Creating
Ipspace: " + $Ipspace)
New-NaNetIpspace-Name $Ipspace
}
catch
{
Throw "Failed to create Ipspace. Message:
" + $_.Exception.Message;
}
 | 
Perl 错误处理
| 准则 | 示例 | 
|---|---|
Perl 不支持 try/catch 块的原生 语言。使用评估块检查和处理错误。请尽可能小地保留评估块。  | 
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",
    $@
);
 |