PowerShell - 例外処理(throw)
意図的に例外を発生させる場合にはthrowを使用する。
try { "try節です" throw "例外発生" } catch [Exception] { "catch節です" } finally { "finary節です" }
PS C:\> $error[0].CategoryInfo Category : OperationStopped Activity : Reason : RuntimeException TargetName : 例外発生 TargetType : String
例外として任意のオブジェクトも渡せる
try { "try節です" throw New-Object regex ".." } catch [Exception] { "catch節です" } finally { "finary節です" }
PS C:\> $error[0].CategoryInfo Category : OperationStopped Activity : Reason : RuntimeException TargetName : .. TargetType : Regex PS C:\> $error[0].TargetObject.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Regex System.Object