![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Specifying a trap statement's exception as a variable Regarding this form of the trap statement: trap [Exception] { <body of the trap statement> } Is there any way to specify the exception in a variable? The grammar available in Bruce Payette's book indicates that the parser is looking for an open square brace, but I don't know how to use a variable for the exception name or type in that case. If I try executing this code: $exception = [System.DivideByZeroException] trap $exception {'Got it!'} 1/$null I get the error "The trap statement was incomplete. The trap statement requires a body." I'm trying to extend Adam Weigert's Try function ( http://weblogs.asp.net/adweigert/arc...s-to-life.aspx ) to accept an exception type as a parameter to give to the trap statement. I read the posting a couple weeks ago about try-catch-finally support coming in the next drop of the PowerShell 2.0 CTP, but that could be months or a year away. Adam's function seems to work pretty well as long as you remember that it is creating new scopes. Thanks, Chuck |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Specifying a trap statement's exception as a variable On Apr 2, 10:30*am, Chuck Heatherly <chuck.heathe...@xxxxxx> wrote: Quote: > Regarding this form of the trap statement: > > trap [Exception] > { > * * <body of the trap statement> > > } > > Is there any way to specify the exception in a variable? *The grammar available in Bruce Payette's book indicates that > the parser is looking for an open square brace, but I don't know how to use a variable for the exception name or type in > that case. If I try executing this code: > > $exception = [System.DivideByZeroException] > > trap $exception {'Got it!'} > 1/$null > > I get the error "The trap statement was incomplete. The trap statement requires a body." > > I'm trying to extend Adam Weigert's Try function > > (http://weblogs.asp.net/adweigert/arc...ershell-try-ca...) > > to accept an exception type as a parameter to give to the trap statement. I read the posting a couple weeks ago about > try-catch-finally support coming in the next drop of the PowerShell 2.0 CTP, but that could be months or a year away. > Adam's function seems to work pretty well as long as you remember that it is creating new scopes. > > Thanks, > Chuck While you could resort to dirty tricks like: -- 8< -- script.ps1 param($ex, [scriptblock]$handler, [scriptblock]$body) invoke-expression "trap $ex { $handler } $body" -- 8< --- script.ps1 [exception] { "error!" } { throw "oops!" } A better approach might be to implement some "exception filtering" in Adam's -Catch scriptblock, like so: } -Catch { if ($_ -is [argumentexception]) { "argument exception" } # handle it else { throw $_ # pass on up } } And bare in mind that you _can_ use variables with the -is operator as demonstrated by: PS> $argex = new-object argumentexception PS> $argex -is [exception] True PS> $ex = [exception] PS> $argex -is $ex True That should be enough info to get you started. Hope this helps, - Oisin PowerShell MVP http://www.nivot.org/ |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Specifying a trap statement's exception as a variable On Apr 2, 12:53*pm, "Oisin (x0n) Grehan [MVP]" <ois...@xxxxxx> wrote: Quote: > On Apr 2, 10:30*am, Chuck Heatherly <chuck.heathe...@xxxxxx> > wrote: > > > > > Quote: > > Regarding this form of the trap statement: Quote: > > trap [Exception] > > { > > * * <body of the trap statement> Quote: > > } Quote: > > Is there any way to specify the exception in a variable? *The grammar available in Bruce Payette's book indicates that > > the parser is looking for an open square brace, but I don't know how to use a variable for the exception name or type in > > that case. If I try executing this code: Quote: > > $exception = [System.DivideByZeroException] Quote: > > trap $exception {'Got it!'} > > 1/$null Quote: > > I get the error "The trap statement was incomplete. The trap statement requires a body." Quote: > > I'm trying to extend Adam Weigert's Try function Quote: Quote: > > to accept an exception type as a parameter to give to the trap statement.. I read the posting a couple weeks ago about > > try-catch-finally support coming in the next drop of the PowerShell 2.0 CTP, but that could be months or a year away. > > Adam's function seems to work pretty well as long as you remember that it is creating new scopes. Quote: > > Thanks, > > Chuck > Hi Chuck, > > While you could resort to dirty tricks like: > > -- 8< -- script.ps1 > > param($ex, [scriptblock]$handler, [scriptblock]$body) > invoke-expression "trap $ex { $handler } $body" > > -- 8< --- > > script.ps1 [exception] { "error!" } { throw "oops!" } > > A better approach might be to implement some "exception filtering" in > Adam's -Catch scriptblock, like so: > > } -Catch { > > * *if ($_ -is [argumentexception]) { "argument exception" } # handle > it > * *else { > * * * *throw $_ * # pass on up > * *} > > } > > And bare in mind that you _can_ use variables with the -is operator as > demonstrated by: > > PS> $argex = new-object argumentexception > PS> $argex -is [exception] > True > PS> $ex = [exception] > PS> $argex -is $ex > True > > That should be enough info to get you started. > > Hope this helps, > > - Oisin > > PowerShell MVPhttp://www.nivot.org/- Hide quoted text - > > - Show quoted text - for the _most derived_ exceptions first! example: PS> $ex = new-object argumentnullexception PS> $ex -is [argumentexception] True PS> $ex -is [argumentnullexception] True e.g. if you test for argumentexception _before_ argumentnullexception, you'll never get to the latter because the latter inherits from the former ;-) - Oisin |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Specifying a trap statement's exception as a variable Quote: > to accept an exception type as a parameter to give to the trap statement. I read the posting a couple weeks ago about > try-catch-finally support coming in the next drop of the PowerShell 2.0 CTP, but that could be months or a year away. > Adam's function seems to work pretty well as long as you remember that it is creating new scopes. drop would be out on or about the week of MMS (end of April). So it isn't months or a year... It's a matter of weeks now! Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| why is a function level variable cleared in the Trap statement | PowerShell | |||
| Exception stack is corrupt when catching and storing the exception | .NET General | |||
| Vista Install Error: Exception Unknown Software Exception | Vista installation & setup | |||
| trap exception | PowerShell | |||
| Multiple trap handlers in a function: variable scoping issues | PowerShell | |||