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 - Traps and Foreach loops

Reply
 
Old 02-13-2007   #1 (permalink)
lawndart@gmail.com


 
 

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 SpecsSystem Spec
Old 02-14-2007   #2 (permalink)
Clint Bodine


 
 

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 SpecsSystem Spec
Old 02-14-2007   #3 (permalink)
Keith Hill


 
 

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 SpecsSystem Spec
Old 02-14-2007   #4 (permalink)
lawndart


 
 

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 SpecsSystem Spec
Old 02-14-2007   #5 (permalink)
lawndart


 
 

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 SpecsSystem Spec
Reply

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


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