Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Specifying a trap statement's exception as a variable

Reply
 
Old 04-02-2008   #1 (permalink)
Chuck Heatherly


 
 

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 SpecsSystem Spec
Old 04-02-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]


 
 

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
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 MVP
http://www.nivot.org/
My System SpecsSystem Spec
Old 04-02-2008   #3 (permalink)
Oisin (x0n) Grehan [MVP]


 
 

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:

> > 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 -
Oh, another caveat for the exception filtering - ensure that you test
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 SpecsSystem Spec
Old 04-02-2008   #4 (permalink)
Marco Shaw [MVP]


 
 

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.
Bruce Payette announced this week at TechMentor that the next 2.0 CTP
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46