Windows Vista Forums

How to TRY and CATCH
  1. #1


    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.

      My System SpecsSystem Spec

  2. #2


    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.


      My System SpecsSystem Spec

  3. #3


    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.
    > --


      My System SpecsSystem Spec

  4. #4


    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

      My System SpecsSystem Spec

How to TRY and CATCH problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Try Catch Brian .NET General 2 12 Apr 2008
catch-up John A Grandy Vista mail 0 02 Mar 2008
Catch-all Greg23 PowerShell 4 14 Aug 2007
I Catch VI Jason Vista hardware & devices 0 03 Oct 2006
.NET Framework catch-22 Richard Helms Vista General 1 12 Jun 2006