![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | why is a function level variable cleared in the Trap statement Is there any reason why the object created within the trap is cleared once it moves out of the trap. Below is an working sample code. In this $objArray will not have the $output object created inside the trap. Once the function is finished execution. Wanted to know why this happens and how to avoid it. function Test { $arr = "Mangoes","Apples" $insideArr = "ABC", "PQR" $objArray = @() foreach($a in $arr) { trap {continue} &{trap{ write-Host "its here" $output = New-Object object Add-Member -inputobject $output -membertype NoteProperty -name "Name" -value "here mangoes" $objArray += $output break} if($a -eq "Mangoes") { throw "Error happening" } else { $output = new-Object Object add-Member -inputObject $output -membertype NoteProperty -name "Name" $a $objArray += $output } write-Host $a} } return $objArray } return test break $objArray = test; foreach($arrItem in $objArray) { $arrItem.Name } |
My System Specs![]() |
| | #2 (permalink) |
| | Re: why is a function level variable cleared in the Trap statement Hi AdityaKir, Variables created inside TRAP are local to the trap scope. You can workaround it by prefixing $objArray calls with a scope name: $script bjArray = @()and $script bjArray += $outputFor more help: PS > help about_scope --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic PowerShell Toolbar: http://tinyurl.com/PSToolbar A> Is there any reason why the object created within the trap is cleared A> once it moves out of the trap. Below is an working sample code. In A> this $objArray will not have the $output object created inside the A> trap. Once the function is finished execution. Wanted to know why A> this happens and how to avoid it. A> A> function Test A> { A> $arr = "Mangoes","Apples" A> $insideArr = "ABC", "PQR" A> $objArray = @() A> foreach($a in $arr) A> { A> trap {continue} A> &{trap{ A> write-Host "its here" A> $output = New-Object object A> Add-Member -inputobject $output -membertype NoteProperty A> -name A> "Name" -value "here mangoes" A> $objArray += $output A> break} A> if($a -eq "Mangoes") A> { A> throw "Error happening" A> } A> else A> { A> $output = new-Object Object A> add-Member -inputObject $output -membertype A> NoteProperty A> -name "Name" $a A> $objArray += $output A> } A> write-Host $a} A> } A> A> return $objArray A> } A> return test A> A> break A> A> $objArray = test; A> A> foreach($arrItem in $objArray) A> { A> $arrItem.Name A> } |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| How to get more information about the error from trap statement | PowerShell | |||
| How to pass exceptions message outside the trap statement | PowerShell | |||
| Specifying a trap statement's exception as a variable | PowerShell | |||
| Multiple trap handlers in a function: variable scoping issues | PowerShell | |||
| return keyword in trap function | PowerShell | |||