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 - PSH Guru needed ! : Trapping terminating errors

Reply
 
Old 07-18-2007   #1 (permalink)
A. Petitjean


 
 

PSH Guru needed ! : Trapping terminating errors

When I try this
trap { "Got it : division by zero !" } 1/$null; "next"

The trap is working successfully :
Got it : division by zero !
Tentative de division par zéro.
Au niveau de ligne : 1 Caractère : 44
+ trap { "Got it : division by zero !" } 1/$ <<<< null; "next"

But when I try this (who's the same thing in theory) :
trap { "Got it : division by zero !" } 1/0; "next"

I got :
Tentative de division par zéro.
Au niveau de ligne : 1 Caractère : 44
+ trap { "Got it : division by zero !" } 1/0 <<<< ; "next"

And my trap is not working !!!

What's wrong !? I have a big headhache with that problem ! Please help me !

Arnaud Petitjean
http://www.powershell-scripting.com
The PowerShell's French Community

My System SpecsSystem Spec
Old 07-18-2007   #2 (permalink)
Roman Kuzmin


 
 

Re: PSH Guru needed ! : Trapping terminating errors

1/0 fails at compile time, so nothing is wrong, don't worry

--
Thanks,
Roman Kuzmin
PowerShellFar and FarNET: http://code.google.com/p/farnet/


My System SpecsSystem Spec
Old 07-18-2007   #3 (permalink)
Kiron


 
 

Re: PSH Guru needed ! : Trapping terminating errors

The trap works when division by a variable which's value is zero.
I'm no guru though

$zero = 0
trap { "Got it : division by zero !" } 1/$zero; "next"

....or in the pipe:
1,6,9,0 | foreach-object {trap { "Got it : division by zero !" } 1/$_ };
"next"

--
Kiron

My System SpecsSystem Spec
Old 07-19-2007   #4 (permalink)
Edengundam


 
 

Re: PSH Guru needed ! : Trapping terminating errors

<PowerShell In Action> metioned this case, I paste the Author note here:

The example here uses 1/$null. The reason for doing this instead of simply
1/0 is because the PowerShell interpreter does something called constant
expression folding. It looks at expressions that contain only constant
values. When it sees one, it evaluates that expression once at compile time
so it doesn¡¯t have to waste time doing it again at runtime. This means that
impossible expressions, such as division by zero, are caught and treated as
parsing errors. Parsing errors can¡¯t be caught and don¡¯t get logged when
they¡¯re entered interactively, so they don¡¯t make for a good example. (If
one
script calls another script and that script has one of these errors, the
calling
script can catch it, but the script being parsed cannot.)

"A. Petitjean" <APetitjean@discussions.microsoft.com>
news:E32C470A-9D56-498C-837B-F15C2A06147B@microsoft.com...
> When I try this
> trap { "Got it : division by zero !" } 1/$null; "next"
>
> The trap is working successfully :
> Got it : division by zero !
> Tentative de division par z¨¦ro.
> Au niveau de ligne : 1 Caract¨¨re : 44
> + trap { "Got it : division by zero !" } 1/$ <<<< null; "next"
>
> But when I try this (who's the same thing in theory) :
> trap { "Got it : division by zero !" } 1/0; "next"
>
> I got :
> Tentative de division par z¨¦ro.
> Au niveau de ligne : 1 Caract¨¨re : 44
> + trap { "Got it : division by zero !" } 1/0 <<<< ; "next"
>
> And my trap is not working !!!
>
> What's wrong !? I have a big headhache with that problem ! Please help me

!
>
> Arnaud Petitjean
> http://www.powershell-scripting.com
> The PowerShell's French Community



My System SpecsSystem Spec
Old 07-21-2007   #5 (permalink)
A. Petitjean


 
 

Re: PSH Guru needed ! : Trapping terminating errors

Ah Ok, I understand now !
Thanks a lot for your help.

Arnaud

"Edengundam" wrote:

> <PowerShell In Action> metioned this case, I paste the Author note here:
>
> The example here uses 1/$null. The reason for doing this instead of simply
> 1/0 is because the PowerShell interpreter does something called constant
> expression folding. It looks at expressions that contain only constant
> values. When it sees one, it evaluates that expression once at compile time
> so it doesn¡¯t have to waste time doing it again at runtime. This means that
> impossible expressions, such as division by zero, are caught and treated as
> parsing errors. Parsing errors can¡¯t be caught and don¡¯t get logged when
> they¡¯re entered interactively, so they don¡¯t make for a good example. (If
> one
> script calls another script and that script has one of these errors, the
> calling
> script can catch it, but the script being parsed cannot.)
>
> "A. Petitjean" <APetitjean@discussions.microsoft.com>
> news:E32C470A-9D56-498C-837B-F15C2A06147B@microsoft.com...
> > When I try this
> > trap { "Got it : division by zero !" } 1/$null; "next"
> >
> > The trap is working successfully :
> > Got it : division by zero !
> > Tentative de division par z¨¦ro.
> > Au niveau de ligne : 1 Caract¨¨re : 44
> > + trap { "Got it : division by zero !" } 1/$ <<<< null; "next"
> >
> > But when I try this (who's the same thing in theory) :
> > trap { "Got it : division by zero !" } 1/0; "next"
> >
> > I got :
> > Tentative de division par z¨¦ro.
> > Au niveau de ligne : 1 Caract¨¨re : 44
> > + trap { "Got it : division by zero !" } 1/0 <<<< ; "next"
> >
> > And my trap is not working !!!
> >
> > What's wrong !? I have a big headhache with that problem ! Please help me

> !
> >
> > Arnaud Petitjean
> > http://www.powershell-scripting.com
> > The PowerShell's French Community

>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Need a guru!!!!!!!!!!!!! General Discussion
Trapping WMI remoting errors and continuing on. PowerShell
Powershell TRAP for non-terminating errors PowerShell
Wooly concepts? Terminating and non-terminating errors. PowerShell
Validation needed: Tabular output throws OutofMemoryException errors with Out-File -Width 0x7fffffff 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