本繁體中文版使用機器翻譯,譯文僅供參考,若與英文版本牴觸,應以英文版本為準。
錯誤處理準則
撰寫OnCommand Workflow Automation 適用於WFA(WFA)的PowerShell或Perl指令碼時、您必須瞭解錯誤處理的準則。
PowerShell錯誤處理
| 準則 | 範例 |
|---|---|
PowerShell執行時間新增至Cmdlet的一般參數包括錯誤處理參數、例如錯誤動作和警告動作:
如需詳細資訊、您可以在PowerShell CLI中使用「Get-Help關於_CommonParameters」命令。 |
錯誤動作:下列範例顯示如何將非終止錯誤當作終止錯誤處理: 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不包含試用/攔截區塊的原生語言支援。使用評估區塊來檢查及處理錯誤。盡量將評估區塊保持在最小的位置。 |
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",
$@
);
|