2012-07-28 49 views
6

這裏是我的代碼:的PowerShell:創建一個自定義異常

Function Foo { 
    If (1 -Eq 2) { 
     # Do stuff 
    } 
    Else { 
     # Throw custom exception 
    } 
} 

Try { 
    Foo 

    Write-Host "Success" 
} 
Catch { 
    $ErrorMessage = $_.Exception.InnerException.Message 

    Write-Host "Failure" 

    # Do stuff with the error message 
} 

我想要的代碼,將導致Catch火更換# Throw custom exception。我怎樣才能做到這一點?

回答

18

不知道我真的得到了你的問題,但似乎所有你想要做的是:

throw "message for the exception" 
+1

我用'$ =的ErrorMessage $ _ Exception.InnerException.Message'因爲我認爲。將與'Throw'一起工作。事實證明,在這種情況下它應該是'$ _。Exception.Message'。謝謝! – Nick 2012-07-29 02:53:16