2012-08-16 11 views
86

PowerShellで新しい例外を作成して投げる方法は?Powershell:新しい例外を作成して投げる

特定のエラーに対してさまざまなことを行いたいと思います。文字列が続く投げコマンドを使用する一般的な例外をスローするには

if (-not (Test-Path $file)) 
{ 
    throw [System.IO.FileNotFoundException] "$file not found." 
} 

をこのフォーマットを使用するなどにFileNotFoundExceptionなどの特定の例外を呼び出すために

+2

は、カスタム例外をスローするために探しているエラーを引き起こしたかについての追加情報を提供することができますか? [this] [1]は助けますか? [1]:http://stackoverflow.com/questions/11703180/powershell-creating-a-custom-exception –

+1

この記事を見てみましょう:http://stackoverflow.com/questions/2182666/powershell-2-0-try-catch-how-to-access-the-exception –

答えて

129

。キャッチの内部で使用する場合

throw "Error trying to do a task" 

は、あなたが

関連する問題