Tripped up on some PowerShell try, catch, functions, and cascading error handling oddness!

Analysis
Mar 22, 20093 mins

Try to say that title five times in a row…

Maybe this posting can be classified as a stupid PowerShell trick.  Or, it’s just me blabbing on about how I wish error handling in PowerShell worked a certain way (it’s always been a sore subject).  Anyhow, I was writing a script and given that CTP3 now supports the good old try and catch error handling methodology.  My error handling logic, of course, was using it.  The problem was… errors were not popping up where I thought they should.

To start us down the path of explaining why I encountered my issue, confusion, or whatever you want to call it.  Let’s first start out with a basic script:

# My Script

try {

    “No Error”

    throw

    “I should never come here”

    }

 

catch {

    “This has been caught”

    }

If you execute the above script, an error is produced, which is then caught.  Straight forward… Now let’s expand the complexity and add in a function:

# My Script with a Function

function myfunction1 {

    “look I’m in a function”

    throw

    }

 

try {

    “No Error”

    myfunction1

    “I should never come here”

    }

 

catch {

    “This has been caught”

    }

Once again, if you execute the script an error is produced, which is then caught.  However, in this case the error is generated within the function and more importantly I never get to the “I should never come here” string.

Now, taking things a little further, let’s do a try and catch within the function:

# My Script with a Function that has try and catch

function myfunction2 {

    try {

        “look I’m in a function”

        throw

        }

    catch {

        return $Error[0]

        }

    }

 

try {

    “No Error”

    myfunction2

    “I should never come here”

    }

 

catch {

    “This has been caught”

    }

And, if you execute the script now you will see the problem that I encounter.  Regardless of the fact that an error was returned from the function, the script continues to execute.  This means, I reach the “I should never come here” string, despite being in an error state that should prevent the continued execution of the script.  Is this a bug, should it work like this, or is my logic just flawed.  For reason, I have in my mind that if a function is called within a try and catch block, and if that function returns an error object.  Then the catch should kick in.

Yes… yes… I know what you are saying.  Just check for an error object, but I’m lazy.

If you like this, check out some other posts from Tyson:

  • Would I trust you, if everyone else trusted you?
  • Here is a good question: Is scripting programming or just systems administration?
  • PowerShell boy and the case of the missing cmdlets!
  • Fun with PowerShell 2.0 Eventing!
  • Creating a custom 404 page to handle link redirection for ASP.NET web applications
  • Microsoft Discontinues Support for Windows 3.X (as an embedded system)
  • My quest for SSH within PowerShell revisited!

Or if you want, you can also check out some of Tyson’s latest publications:

Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet.  Or, sign up for the bi-weekly Microsoft newsletter.  (Click on News/Microsoft News Alert)