在PS

2012-10-24 37 views
1
檢測一個.NET異常

我想提出幾個調用.NET類,以便在我的PS這樣的腳本:在PS

[class1]::MethodA() 
[class1]::MethodB() 

if(/*check if last method threw an error*/) 
{ 
    "MethodB failed! Exiting." 
    return 
} 

[class2]::MethodC() 
[class2]::MethodD() 

我真的要檢查是否有從方法b異常移動之前。如何檢查這個?

回答

1

使用try/catch塊:

try 
{ 
[class1]::MethodB() 
} 
catch 
{ 
Write-Host "MethodB failed!" 
exit 
}