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 - trapping oddity

Reply
 
Old 09-29-2006   #1 (permalink)
klumsy@xtra.co.nz


 
 

trapping oddity

where we run:

function a
{
trap { write-host ("trapped " + $_.Exception.GetType().Name); continue;
}
write-host "starting b"
write-host ($(1 / $null))
write-host "ending b"
}

a

despite the exception being trapped and shown as trapped, the ending b
is written. i think it shouldn't do that..

however

function a
{
trap { write-host ("trapped " + $_.Exception.GetType().Name); continue;
}

}

function b
{
write-host "starting b"
write-host ($(1 / $null))
write-host "ending b"

}

a

runs as expected (it doesn't print the "ending b"

Karl


My System SpecsSystem Spec
Old 09-29-2006   #2 (permalink)
Bruce Payette [MSFT]


 
 

Re: trapping oddity

Just to check: I think that you meant to write the second a function as:

function a
{
trap { write-host ("trapped " + $_.Exception.GetType().Name); continue; }
b # - this was missing...
}

Anyway, traps are scoped to the statement block containing the trap. On an
exception, trap/continue transfers control to the next statement in *that*
block. So - if the exception occurs in a function call, the rest of that
function will be skipped and control will continue at the next statement
after where the function was called. There is a very useful pattern that
takes advantage of this:

PS (1) > & {
>> trap { "Got error" ; continue }
>> . {
>> "first"
>> "second"
>> 1/$null
>> "third"
>> "fourth"
>> }
>> "Finally"
>> }
>>

first
second
Got error
Finally
PS (2) >


By using the scriptblock to scope a set of statements, you can skip to the
end of the set instead of having to write a bunch of conditionals. This is
the effective equivalent to

try {
"first"
"second"
1/$null
"third"
}
catch {
"Got error"
}
finally {
"Finally"
}

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



<klumsy@xtra.co.nz> wrote in message
news:1159545338.913882.81790@h48g2000cwc.googlegroups.com...
> where we run:
>
> function a
> {
> trap { write-host ("trapped " + $_.Exception.GetType().Name); continue;
> }
> write-host "starting b"
> write-host ($(1 / $null))
> write-host "ending b"
> }
>
> a
>
> despite the exception being trapped and shown as trapped, the ending b
> is written. i think it shouldn't do that..
>
> however
>
> function a
> {
> trap { write-host ("trapped " + $_.Exception.GetType().Name); continue;
> }
>
> }
>
> function b
> {
> write-host "starting b"
> write-host ($(1 / $null))
> write-host "ending b"
>
> }
>
> a
>
> runs as expected (it doesn't print the "ending b"
>
> Karl
>



My System SpecsSystem Spec
Old 09-29-2006   #3 (permalink)
klumsy@xtra.co.nz


 
 

Re: trapping oddity

thanks bruce, that was good info..
yep i typed in the example wrong.. my bad

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Desktop Oddity Vista security
Subst oddity with UAC Vista file management
RTM DVD oddity Vista hardware & devices
Trapping exceptions PowerShell
Tab Completion oddity 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