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

Error Trapping : doesn't work

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-05-2007   #1 (permalink)
dm_14
Guest


 

Error Trapping : doesn't work

If I do..

trap [System.DivideByZeroException]
{
write-host ("trapped " + $_.Exception.GetType().Name)
continue
}
(1/$null)

it works!. If I do

trap [System.DivideByZeroException]
{
write-host ("trapped " + $_.Exception.GetType().Name)
continue
}
(1/0)

it doesn't . Yet the inner exception description for both are the same. Any
thoughts ?

My System SpecsSystem Spec
Old 09-05-2007   #2 (permalink)
Kiron
Guest


 

Re: Error Trapping : doesn't work

That happens because the expression 1/0 gets evaluated at compile time.
PowerShell does 'constant expression folding', which roughly means that if
there is an expression with a constant value like 1/0 (both constant values)
the error is caught at compile time instead of runtime.

--
Kiron

My System SpecsSystem Spec
Old 09-05-2007   #3 (permalink)
dm_14
Guest


 

Re: Error Trapping : doesn't work

Thanks for the quick response.

"Kiron" wrote:
Quote:

> That happens because the expression 1/0 gets evaluated at compile time.
> PowerShell does 'constant expression folding', which roughly means that if
> there is an expression with a constant value like 1/0 (both constant values)
> the error is caught at compile time instead of runtime.
>
> --
> Kiron
>
>
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
trapping a particular type of error andy-powershell.com PowerShell 4 08-27-2008 12:42 PM
Another error trapping problem Rob Campbell PowerShell 2 01-25-2008 07:06 PM
trapping error messages froom get-qaduser blittle PowerShell 3 07-09-2007 09:30 AM
Help w/ error trapping Blip PowerShell 3 02-12-2007 07:34 PM
trapping error and then continue IT Staff PowerShell 1 01-23-2007 03:41 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