![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Traps and Foreach loops I'm trying to to use trap to catch an error and set what part of a loop executes depending on the error state. I can catch the error and set a variable, but it bails out of my loop right after that no matter what I end the trap with (continue, break, or return [x]). Here is a nice snippet of the problem: trap { $script:bolSkip = $true "Exception caught" continue } foreach($srv in (1,2)){ $bolSkip = $false if($srv -eq 1){ throw } if($bolSkip -eq $true){ "Error, Skipping"} else{ "Normal Execution" } } "Done" This should "Error, Skipping" once, continue and "Normal Execution" once. Instead the whole foreach bails out after the exception. What am I missing about traps? Lawndart |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Traps and Foreach loops On Feb 13, 3:41 pm, lawnd...@gmail.com wrote: > I'm trying to to use trap to catch an error and set what part of a > loop executes depending on the error state. I can catch the error and > set a variable, but it bails out of my loop right after that no matter > what I end the trap with (continue, break, or return [x]). I ran into this same problem today. The problem is with the scope. Check out this note from Windows Powershell: TFM by Don Jones and Jeffery Hicks (http://www.sapienpress.com/powershell.pdf) : "When you exit a trap handler using Continue, the next line of code in the same scope is executed. The Break keyword exits the current scope and goes up one level to the calling scope, passing the exception up to the calling scope (where another trap handler, if one is defined, can be called). The Return keyword does more or less the same thing, except that the specified argument is passed to the calling scope and no exception is thrown. So, if the trap handler lives inside a function, the Return keyword will simply append to the function's return value, and the function will exit normally as if nothing bad occurred." So put your trap catcher in the same scope as you want the next line executed. As ugly as it is, it fixed my problem. foreach($srv in (1,2)){ $bolSkip = $false trap { $script:bolSkip = $true "Exception caught" continue} if($srv -eq 1){ throw } if($bolSkip -eq $true){ "Error, Skipping"} else{ "Normal Execution" }} "Done" |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Traps and Foreach loops <lawndart@gmail.com> wrote in message news:1171406512.564896.37020@v33g2000cwv.googlegroups.com... > I'm trying to to use trap to catch an error and set what part of a > loop executes depending on the error state. I can catch the error and > set a variable, but it bails out of my loop right after that no matter > what I end the trap with (continue, break, or return [x]). > > Here is a nice snippet of the problem: > > trap { > $script:bolSkip = $true > "Exception caught" > continue > } > foreach($srv in (1,2)){ > $bolSkip = $false > if($srv -eq 1){ throw } > if($bolSkip -eq $true){ > "Error, Skipping"} > else{ > "Normal Execution" } > } > "Done" > > This should "Error, Skipping" once, continue and "Normal Execution" > once. Instead the whole foreach bails out after the exception. What am > I missing about traps? Try this: foreach($srv in (1,2)){ $bolSkip = $false trap { $script:bolSkip = $true; 'Exception caught'; continue } & { if ($srv -eq 1) { throw } } if ($bolSkip) { "Error, Skipping" } else { "Normal Execution" } } "Done" What you are missing is that when a "continue" is specified, execution resumes at the next instruction in the same scope as the trap statement. Using the & { } allows us to create a scope to execute a number of comands which if throw, will result in the next line after the & {} scope. -- Keith |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Traps and Foreach loops Thanks, I totally missed the scope dependent aspect of trap. I just assumed it was a global type thing. Now I just need to add "make a scope-aware trap function" to my long term powershell goals ![]() Lawndart |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Traps and Foreach loops On Feb 14, 7:59 am, "lawndart" <lawnd...@gmail.com> wrote: > Thanks, I totally missed the scope dependent aspect of trap. I just > assumed it was a global type thing. Now I just need to add "make a > scope-aware trap function" to my long term powershell goals ![]() > > Lawndart Actually on that note, I guess I have a further question. What exactly is happening with the above code? Is the throw immediately calling the trap from the foreach scope, which then continues from the trap's scope, or is the throw propagating up out of the foreach scope into the script's scope, and then being caught by the trap, which then continues from where it was "called", the script scope. It sounds like the second option is the most likely, but someone more intimately involved with the language might know better. Lawndart (eagerly awaiting his books on the language so he doesn't have to ask basic questions) |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Moving Personal Folders: Not Intuitive and With Serious Traps | Vista file management | |||
| foreach, foreach-object & begin/process/end scriptblock clauses... | PowerShell | |||
| Difference in semantics of for, foreach and foreach-object | PowerShell | |||
| possible bug with using the $foreach keyword inside foreach loops.. | PowerShell | |||
| Common PowerShell programming traps | PowerShell | |||