![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | Re: trapping oddity thanks bruce, that was good info.. yep i typed in the example wrong.. my bad |
My System Specs![]() |
![]() |
| 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 | |||