Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Can TRAP be redeclared?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-22-2006   #1 (permalink)
Maxim Maslov
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 SpecsSystem Spec
Old 12-22-2006   #2 (permalink)
RichS
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 SpecsSystem Spec
Old 12-22-2006   #3 (permalink)
Maxim Maslov
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 SpecsSystem Spec
Old 12-22-2006   #4 (permalink)
Roman Kuzmin
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 SpecsSystem Spec
Old 12-22-2006   #5 (permalink)
/\/\o\/\/ [MVP]
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 SpecsSystem Spec
Old 12-22-2006   #6 (permalink)
Maxim Maslov
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 SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

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


Vistax64.com 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 2005-2008

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 47 48 49 50 51