![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Can TRAP be redeclared? How can I redeclare TRAP block in the current scope? The following script outputs "Trap 1" two times: trap { "Trap 1."; continue; }; $a = 1 / $null; trap { "Trap 2."; break; }; $a = 2 / $null; Regards, Maxim |
My System Specs![]() |
| | #2 (permalink) |
| Guest | RE: Can TRAP be redeclared? Your 2 trap commands are both set to catch all exceptions. In this cse the first one defined will be the one called. If you try this variation of your script trap { "Trap 1."; continue; }; $a = 1 / $null; trap [DivideByZeroException]{ "Trap 2."; continue; }; $a = 2 / $null; You will see that Trap 2 is used in both instances. So the answer to your question appears to be yes you can redefine traps but if you define 2 traps with the same exception in the same scope the first defined will be used -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty "Maxim Maslov" wrote: > How can I redeclare TRAP block in the current scope? > > The following script outputs "Trap 1" two times: > > trap { "Trap 1."; continue; }; > $a = 1 / $null; > trap { "Trap 2."; break; }; > $a = 2 / $null; > > Regards, > Maxim > > > |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Can TRAP be redeclared? So the answer is 'No' ? ![]() We can define different traps for different exceptions but cannot redefine trap for particular exception. So 'trap' is not 'executable' statement (in contast to function definition), but just declaration that can be located in any place. Am I right? Regards, Maxim "RichS" <RichS@discussions.microsoft.com> wrote in message news:F8241D5A-B376-4F3C-BF8C-F2B60F39528E@microsoft.com... > Your 2 trap commands are both set to catch all exceptions. In this cse > the > first one defined will be the one called. If you try this variation of > your > script > > trap { "Trap 1."; continue; }; > $a = 1 / $null; > trap [DivideByZeroException]{ "Trap 2."; continue; }; > $a = 2 / $null; > > You will see that Trap 2 is used in both instances. So the answer to your > question appears to be yes you can redefine traps but if you define 2 > traps > with the same exception in the same scope the first defined will be used > -- > Richard Siddaway > > Please note that all scripts are supplied "as is" and with no warranty > > > "Maxim Maslov" wrote: > >> How can I redeclare TRAP block in the current scope? >> >> The following script outputs "Trap 1" two times: >> >> trap { "Trap 1."; continue; }; >> $a = 1 / $null; >> trap { "Trap 2."; break; }; >> $a = 2 / $null; >> >> Regards, >> Maxim >> >> >> |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Can TRAP be redeclared? "Maxim Maslov" <maslov@soft-consult.ru> wrote in message news:u$mrNybJHHA.1816@TK2MSFTNGP06.phx.gbl... > So the answer is 'No' ? ![]() Probably you are right (i.e. the answer is 'No'). Unfortunately trapping mechanism is perhaps the least documented PowerShell feature and the one with some oddities discussed here and there occasionally. -- Thanks, Roman Kuzmin |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Can TRAP be redeclared? You can rethrow the trap in the first handler if it can not handle it Greetings /\/\o\/\/ "Roman Kuzmin" wrote: > "Maxim Maslov" <maslov@soft-consult.ru> wrote in message > news:u$mrNybJHHA.1816@TK2MSFTNGP06.phx.gbl... > > So the answer is 'No' ? ![]() > > Probably you are right (i.e. the answer is 'No'). Unfortunately trapping > mechanism is perhaps the least documented PowerShell feature and the one > with some oddities discussed here and there occasionally. > > -- > Thanks, > Roman Kuzmin > > > |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Can TRAP be redeclared? Ok, thanks. I just was interested whether it's possible to define different exception processing in different parts of a script (like try/catch or 'on error' in VB) without wraping these parts into functions, etc. Regards, Maxim "/\/\o\/\/ [MVP]" <oMVP@discussions.microsoft.com> wrote in message news:0AB64C9D-8AEB-4882-9784-00A65FFD65F5@microsoft.com... > You can rethrow the trap in the first handler if it can not handle it > > Greetings /\/\o\/\/ > > > "Roman Kuzmin" wrote: > >> "Maxim Maslov" <maslov@soft-consult.ru> wrote in message >> news:u$mrNybJHHA.1816@TK2MSFTNGP06.phx.gbl... >> > So the answer is 'No' ? ![]() >> >> Probably you are right (i.e. the answer is 'No'). Unfortunately trapping >> mechanism is perhaps the least documented PowerShell feature and the one >> with some oddities discussed here and there occasionally. >> >> -- >> Thanks, >> Roman Kuzmin >> >> >> |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to trap exceptions? | schadek | PowerShell | 0 | 05-28-2008 09:54 PM |
| How to Stop Trap | Peter Sun | PowerShell | 2 | 02-15-2008 01:10 PM |
| trap scope | Bob Butler | PowerShell | 9 | 10-27-2007 08:41 PM |
| Trap about Gwmi | HungYuWu | PowerShell | 2 | 03-21-2007 08:32 PM |
| trap and Get-WmiObject | rockmoose | PowerShell | 3 | 12-25-2006 04:59 AM |