Documenting TRAP and THROW

Rate This
  • Comments 14

We pride ourselves about being a bit overboard when it comes to Error handling.  That is why our sparse (non-existing?) documentation of THROW and TRAP handling is both ironic and sad.  10,000 apologizes for that, we'll fix it in the next release. 

In the mean time, it's Don Jones amd Jeffery Hicks to the rescue.  Don and Jeffery are the co-authors of Windows PowerShell: TFM.  The great thing about that is that they decided to "give you a taste" of the book so you can see whether you might be interesting in buying it.  It is our collective good luck that they made Chapter 11 Debugging and Error Handling as the sample chapter.  Click on the Chapter in the previous sentence and it will bring you to the chapter.  If that doesn't work - go to: .

BTW - their book recently went to Press so it should be available any day now.  I can't wait to get my copy.


Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:
Visit the Windows PowerShell ScriptCenter at:

Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • Nice but Don and Jeffery could not work out the syntax for:

    Trap [ExceptionType] {

    either it would seem as they don't have an example of how to express [ExceptionType]

    nor does Bruce Payette

    Sorry, but if they can't work it out how can I?


  • Umm.

    Try this in a script file:

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


    It works just fine

  • So why do you care about debugging powershell scripts? Umm unless you have the superhuman ability to

  • Fyi, the sample chapters at Sapien are chapter 1 and 9 (formatting) NOT chapter 11 (trap and throw).

  • I have to say it: LAME!

    It's almost like you guys went out of your way to make sure that all the PowerShell developers and their buddies could make some extra money writing books to fill the voids in your documentation.

    What's worse is that this is the first Google hit for powershell trap ... is there anywhere where there is *actual* document about it, instead of a broken link?


  • The book excerpt says "Trap handlers always execute, no matter what ErrorAction you specify"  

    It seems some commands require -ea 'stop' on the command or set globally with $ErrorActionPreference = "Stop"

  • How's this for an attempt?

  • The link to is busted

  • The link to the pdf is not working :( Any one have another link I can try?

    Cheers for all the hard work!


  • This is a great example of how to use Trap and Throw in PowerShell to handle errors besides using SilentlyContinue

  • Joel "Jaykul" Bennett, that last link to was just perfect.  Thanks a ton.

  • Why doesn't this trap the "Generic Failure"?


    Function Test-Trap1() {

      trap [Exception] {


         write-error $("TRAPPED: " + $_.Exception.GetType().FullName);

         write-error $("TRAPPED: " + $_.Exception.Message);



    $credentials=Get-Credential 'CONTOSO106\Administrator'

    if (-not $credentials) {return}

    Write-Host "credentials exist."

    $SqlCmd=Get-WmiObject `

    -Credential $credentials `

    -ComputerName 'IDM-SCCM2' `

    -Namespace 'ROOT\sms\site_IDM' `

    -Class 'SMS_SCI_SQLCmd'

      write-host "done.";







       credentials exist.

       Generic failure

       At :line:12 char:22

       + $SqlCmd=Get-WmiObject <<<<   `



  • @SMerrill8.

    My guess is that the error is a NON-TERMINATING error so it is not caught by the TRAP.  You can convert a non-terminating error into a terminating error (so it is caught by the TRAP) by adding "-ErrorAction STOP" to the Get-WMIOBject cmdlet.  

    GIve that a try and let me know what happens.


    Jeffrey Snover [MSFT]

    Distinguished Engineer

    Visit the Windows PowerShell Team blog at:

    Visit the Windows PowerShell ScriptCenter at:

Page 1 of 1 (14 items)

Documenting TRAP and THROW