none
Errors, Exceptions, and how to handle them RRS feed

  • Question

  • I'm after some advice, best practises if you will, for how to do error handling. I note from Azure Automation: Controlling Runbook Streams for Testing and Troubleshooting that:

    • Errors are different from exceptions – by default errors will not stop runbook execution, whereas uncaught exceptions will cause the runbook to suspend.

    So, I have some questions arising from that. It sys "by default errors won't stop runbook execution". What is the correct way to change that default behaviour?

    The reason I ask is that I just ran a runbook that does many things, one of which is to provision an HDInsight cluster. The provisioning failed and I got an error in Azure Automation:

    However execution simply carried on its merry way which is not acceptable.

    What is the correct way of handling this? Should I be wrapping all my code in try..catch? If so, I'm left wondering which how I should handle non-terminating error. My understanding is that non-terminating errors do not trigger the catch{} block, so anything particular I should do in those situations.

    Any opinions most welcome.

    thanks
    JT

    Friday, November 28, 2014 2:55 PM

Answers

  • Hi Jamie,

    You can use the following to change the behavior of PowerShell Workflow so that errors are thrown like exceptions, causing the runbook to suspend:

    $ErrorActionPreference = "Stop"

    Or specify the same on a single activity, using the ErrorAction parameter:

    Write-Error "hi" -ErrorAction Stop

    At this point, if you surround the command in a try / catch as usual, execution will go to the catch block if an error is hit, like it would with an exception.

    • Marked as answer by Joe Levy_ Friday, November 28, 2014 8:53 PM
    Friday, November 28, 2014 8:53 PM

All replies

  • Hi Jamie,

    You can use the following to change the behavior of PowerShell Workflow so that errors are thrown like exceptions, causing the runbook to suspend:

    $ErrorActionPreference = "Stop"

    Or specify the same on a single activity, using the ErrorAction parameter:

    Write-Error "hi" -ErrorAction Stop

    At this point, if you surround the command in a try / catch as usual, execution will go to the catch block if an error is hit, like it would with an exception.

    • Marked as answer by Joe Levy_ Friday, November 28, 2014 8:53 PM
    Friday, November 28, 2014 8:53 PM
  • Ah I see, thanks Joe. I knew about $ErrorActionPreference=Stop but assumed (wrongly) that that would literally stop execution even if inside a try... catch.

    Seems like my right approach is to set $ErrorActionPreference=Stop, use try catch, and throw the exception back to the calling runbook (we have quite a deep hierarchy now of runbooks calling other runbooks).

    Saturday, November 29, 2014 11:15 PM
  • Hi Jamie ,

    I have faced same issue like this . Got resolved by the above link

    Catching Specific Exceptions

    Now, as our example stands we are catching any errors that occur during the file read and dealing with all of them in the same way. You can however catch specific exceptions and deal with them differently, but – and it’s a big but – only if the original error is terminating. Because the Get-Content cmdlet throws non-terminating errors (that we have only treated as terminating using ErrorAction) we cannot specifically catch the different exceptions that the cmdlet might throw. This is a feature of PowerShell and applies to any non-terminating error, regardless of the ErrorActionPreference and cannot be changed. Still, we can deal with other terminating exceptions, such as an out of memory error, that could crop up during the read operation. For the purposes of this example that is what we will do.

    You catch specific terminating errors by specifying the exception name immediately after the Catch keyword. In our example we want to catch a System.OutOfMemory exception and, if we get one, will take the no nonsense approach of rebooting the computer immediately. We will also include a general catch block after our file not found block to catch all other exceptions:

    Try
    {
        $AuthorizedUsers= Get-Content \\ FileServer\HRShare\UserList.txt -ErrorAction Stop
    }
    Catch [System.OutOfMemoryException]
    {
        Restart-Computer localhost
    }
    Catch
    {
        $ErrorMessage = $_.Exception.Message
        $FailedItem = $_.Exception.ItemName
        Break
    }


    Thursday, August 8, 2019 1:12 PM