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

How to TRY and CATCH

Closed Thread
 
Thread Tools Display Modes
Old 12-21-2006   #1 (permalink)
John Smith
Guest


 

How to TRY and CATCH

HELP ABOUT_TRAP produces nothing. Scanning output of HELP for TRAP and
EXCEPTION produces nothing.

How do I handle exceptions in Windows PowerShell?

Thanks.
Old 12-21-2006   #2 (permalink)
Tom G.
Guest


 

Re: How to TRY and CATCH

In PowerShell you use the trap statement to handle errors. There's a
little info about traps in the about_break help file as well as in the
Tri-fold reference that's installed with PowerShell (just the basic
structure of the trap statement really).

A better source is the PowerShell team blog. There's a four or five
part series about error handling in (at the time Monad, but it all
still holds true) PowerShell. You can find the first blog in the series
here: http://blogs.msdn.com/monad/archive/...04/489138.aspx

Tom G.
--



John Smith wrote:

> HELP ABOUT_TRAP produces nothing. Scanning output of HELP for TRAP
> and EXCEPTION produces nothing.
>
> How do I handle exceptions in Windows PowerShell?
>
> Thanks.

Old 12-21-2006   #3 (permalink)
John Smith
Guest


 

Re: How to TRY and CATCH

Thanks. But can't find part 9 which talks about trapping errors.

"Tom G." wrote:

> A better source is the PowerShell team blog. There's a four or five
> part series about error handling in (at the time Monad, but it all
> still holds true) PowerShell. You can find the first blog in the series
> here: http://blogs.msdn.com/monad/archive/...04/489138.aspx
>
> Tom G.
> --

Old 12-22-2006   #4 (permalink)
John Smith
Guest


 

Re: How to TRY and CATCH

Found the article. But it's by examples. What goes into the [ ]? I tried[*] and it didn't work. I tried [] and it didn't work either.

In a function, it appears that RETURN or BREAK in a TRAP block does not exit
the function:

PS C:\> function validate-imagefile([String]$file)
>> {
>> trap [System.SystemException] {
>> write-host "Error is " $_ -foreground green
>> return (1 -eq 2); }
>> $img = New-Object -typename System.Drawing.Bitmap -ArgumentList $file
>> Remove-variable img
>> return (1 -eq 1)
>> }
>> validate-imagefile("xxx")
>>

Error is Exception calling ".ctor" with "1" argument(s): "Parameter is not
valid."
New-Object : Exception calling ".ctor" with "1" argument(s): "Parameter is
not valid."
At line:6 char:20
+ $img = New-Object <<<< -typename System.Drawing.Bitmap -ArgumentList
$file
Remove-Variable : Cannot find a variable with name 'img'.
At line:7 char:18
+ Remove-variable <<<< img
False
True
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newsgroups won't catch up John R Vista mail 12 05-09-2008 08:00 AM
Try Catch Brian .NET General 2 04-12-2008 12:48 PM
catch-up John A Grandy Vista mail 0 03-02-2008 07:53 PM
Catch-all Greg23 PowerShell 4 08-14-2007 02:18 AM
I Catch VI Jason Vista hardware & devices 0 10-03-2006 07:01 PM








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