about_Trap
適用対象: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0
トピック
about_Trap
概要
終了エラーを処理するキーワードについて説明します。
詳細説明
終了エラーが発生すると、ステートメントの実行が停止します。Windows PowerShell® で終了エラーに対して何も処理を行わない場合、Windows PowerShell によって現在のパイプラインの関数またはスクリプトの実行も停止されます。C# などのその他の言語では、終了エラーは例外と呼ばれます。
Trap キーワードは、終了エラーが発生したときに実行するステートメントの一覧を指定します。Trap ステートメントは終了エラーを処理し、スクリプトまたは関数の実行を停止するのではなく、続行できるようにします。
構文
Trap ステートメントの構文は次のとおりです。
trap [[<error type>]] {<statement list>}
Trap ステートメントには、終了エラーが発生したときに実行するステートメントの一覧が含まれます。Trap キーワードでは、オプションでエラー タイプを指定できます。エラー タイプには、角かっこが必要です。
1 つのスクリプトまたはコマンドに、複数の Trap ステートメントを指定できます。Trap ステートメントは、スクリプトまたはコマンドの任意の場所に配置できます。
すべての終了エラーのトラップ
他の方法で処理できない終了エラーがスクリプトまたはコマンドで発生した場合、Windows PowerShell はエラーを処理する Trap ステートメントがないかどうかを確認します。Trap ステートメントが存在する場合、Windows PowerShell は Trap ステートメントのスクリプトまたはコマンドの実行を続けます。
次に、簡単な Trap ステートメントの例を示します。
trap {"Error found."}
この Trap ステートメントはすべての終了エラーをトラップします。次に、この Trap ステートメントを含む関数の例を示します。
function TrapTest {
trap {"Error found."}
nonsenseString
}
この関数には、エラーを発生させる無意味な文字列が含まれています。この関数を実行すると、次のように返されます。
C:\PS> TrapTest
Error found.
次の例には、$_ 自動変数を使用してエラーを表示する Trap ステートメントが含まれています。
function TrapTest {
trap {"Error found: $_"}
nonsenseString
}
このバージョンの関数を実行すると、次のように返されます。
C:\PS> TrapTest
Error found: The term 'nonsenseString' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included verify that the path
is correct, and then try again.
Trap ステートメントをさらに複雑にすることもできます。Trap ステートメントに複数の条件または関数呼び出しを含めることができます。ログの記録、テスト、または別のプログラムの実行を行うこともできます。
特定の終了エラーのトラップ
次に、CommandNotFoundException エラー タイプをトラップする Trap ステートメントの例を示します。
trap [System.Management.Automation.CommandNotFoundException]
{"Command error trapped"}
関数またはスクリプトで既知のコマンドに一致しない文字列が検出されると、この Trap ステートメントによって "Command error trapped" という文字列が表示されます。Trap ステートメントの一覧のすべてのステートメントを実行した後、Windows PowerShell はエラー オブジェクトをエラー ストリームに書き込んで、スクリプトを続行します。
Windows PowerShell では Microsoft .NET Framework の例外タイプが使用されます。次に、System.Exception エラー タイプを指定する例を示します。
trap [System.Exception] {"An error trapped"}
CommandNotFoundException エラー タイプは、System.Exception タイプから継承します。このステートメントでは、不明なコマンドによって作成されるエラーをトラップします。また、その他のエラー タイプもトラップします。
1 つのスクリプトに複数の Trap ステートメントを設定できます。それぞれのエラーは、1 つの Trap ステートメントでのみトラップできます。エラーが発生し、複数の Trap ステートメントが使用可能になっている場合、Windows PowerShell は、そのエラーに一致する最も的確なエラー タイプの Trap ステートメントを使用します。
次のスクリプトの例には、エラーが含まれています。スクリプトには、すべての終了エラーをトラップする一般的な Trap ステートメントと、CommandNotFoundException タイプを指定した特定の Trap ステートメントが含まれています。
trap {"Other terminating error trapped" }
trap [System.Management.Automation.CommandNotFoundException] {"Command error trapped"}
nonsenseString
このスクリプトを実行すると、次の結果が生成されます。
Command error trapped
The term 'nonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of
the name, or if a path was included verify that the path is correct,
and then try again.
At C:\PS>testScript1.ps1:3 char:19
+ nonsenseString <<<<
Windows PowerShell では "nonsenseString" をコマンドレットまたはその他の項目として認識しないため、CommandNotFoundException エラーが返されます。この終了エラーは、特定の Trap ステートメントでトラップされます。
次のスクリプトの例には、同じ Trap ステートメントと別のエラーが含まれています。
trap {"Other terminating error trapped" }
trap [System.Management.Automation.CommandNotFoundException]
{"Command error trapped"}
1/$null
このスクリプトを実行すると、次の結果が生成されます。
Other terminating error trapped
Attempted to divide by zero.
At C:PS> errorX.ps1:3 char:7
+ 1/ <<<< $null
0 で除算しようとした場合、CommandNotFoundException エラーは作成されません。代わりに、そのエラーは、すべての終了エラーをトラップする別の Trap ステートメントでトラップされます。
エラーのトラップとスコープ
Trap ステートメントと同じスコープで終了エラーが発生した場合、Trap ステートメントの実行後、Windows PowerShell はそのエラーの次のステートメントから続行します。Trap ステートメントがエラーとは異なるスコープにある場合、Trap ステートメントと同じスコープ内にある次のステートメントから実行が続けられます。
たとえば、関数でエラーが発生し、その関数に Trap ステートメントが含まれる場合、スクリプトはその次のステートメントから続行されます。たとえば、次のスクリプトにはエラーと Trap ステートメントが含まれています。
function function1 {
trap { "An error: " }
NonsenseString
"function1 was completed"
}
Function1 関数を実行すると、スクリプトの後半で次の結果が生成されます。
function1
An error:
The term 'NonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included verify that the path is correct, and
then try again.
At C:\PS>TestScript1.ps1:3 char:19
+ NonsenseString <<<<
function1 was completed
関数の Trap ステートメントでエラーがトラップされます。メッセージを表示した後、Windows PowerShell は関数の実行を再開します。Function1 が完了していることに注意してください。
この例を、同じエラーと Trap ステートメントを含む次の例と比較します。次の例では、Trap ステートメントは関数の外部で発生します。
function function2 {
NonsenseString
"function2 was completed"
}
trap { "An error: " }
. . .
function2
Function2 関数を実行すると、スクリプトの後半で次の結果が生成されます。
An error:
The term 'NonsenseString' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included verify that the path is correct, and
then try again.
At C:\PS>TestScript2.ps1:4 char:19
+ NonsenseString <<<<
この例では、"function2 was completed" というコマンドは実行されていません。どちらの終了エラーも関数の内部で発生していますが、Trap ステートメントが関数の外部にある場合、Windows PowerShell は Trap ステートメントの実行後に関数に戻りません。
BREAK キーワードと CONTINUE キーワードの使用
Break キーワードと Continue キーワードを Trap ステートメントで使用して、終了エラー後にスクリプトまたはコマンドの実行を続けるかどうかを決定できます。
Trap ステートメントの一覧に Break ステートメントを含めた場合、Windows PowerShell は関数またはスクリプトを停止します。次のサンプル関数では、Trap ステートメントで Break キーワードを使用しています。
C:\PS> function break_example {
trap {"Error trapped"; break;}
1/$null
"Function completed."
}
C:\PS> break_example
Error trapped
Attempted to divide by zero.
At line:4 char:7
Trap ステートメントに Break キーワードが含まれているため、関数は実行を続けず、"Function completed" の行は実行されません。
Trap ステートメントに Continue ステートメントを含めた場合、Windows PowerShell はエラーの原因となったステートメントの後、Break または Continue を指定していない場合と同様に再開します。ただし、Continue キーワードを使用した場合、Windows PowerShell はエラー ストリームにエラーを書き込みません。
次のサンプル関数では、Trap ステートメントで Continue キーワードを使用しています。
C:\PS> function continue_example {
trap {"Error trapped"; continue;}
1/$null
"Function completed."}
C:\PS> continue_example
Error trapped
Function completed.
関数は、エラーがトラップされた後再開され、"Function completed" ステートメントが実行されます。エラーはエラー ストリームに書き込まれません。
関連項目
about_Break
about_Continue
about_Scopes
about_Throw
about_Try_Catch_Finally