View Single Post
Old 10-13-2006   #3 (permalink)
Andrew Webb


 
 

RE: Multiple trap handlers in a function: variable scoping issues

OTOH... the following works, without having to use 'new-variable'. Sometimes
dot-sourcing is the right thing to do, and at other times it's not. You
really have to know. I'm getting there...

function Main
{
.{
trap
{
write-host "Exception handler 1" + $_.Exception.Message
continue
}
.{
$fi = new-object -typename System.IO.FileInfo -argumentlist "C:\out.txt"
}
}
if ($fi -eq $null)
{
return
}


.{
trap
{
write-host "Exception handler 2" + $_.Exception.Message
continue
}
.{
$fi1 = new-object -typename System.IO.FileInfo -argumentlist
"C:\out1.txt"
}
}
if ($fi1 -eq $null)
{
return
}


write-host $fi.Exists
write-host $fi1.Exists

}

Main # the same as doing: & Main

My System SpecsSystem Spec