Advertisement
Old-Lost

Test -ErrorAction and Try/Catch

Jul 20th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function TestError {
  2.     [cmdletbinding()]
  3.     param([switch]$NonTerminating)
  4.     # Write-Host 'Entering function' -ForegroundColor Yellow
  5.     if ($NonTerminating) {
  6.         Write-Error 'Non-terminating error; function execution continues'
  7.     } else {
  8.         throw [System.IO.IOException]'Terminating error; function execution ends'
  9.     }
  10. }
  11. cls
  12. & {
  13.     trap { Write-Host '!!! Non-terminating error trapped. This should not happen' -ForegroundColor Red; break }
  14.     Write-Host '*** Non-terminating error with try/catch, with -EA Stop ***'
  15.     try { TestError -NonTerminating -ErrorAction Stop; Write-Host '!!! Error not caught' -ForegroundColor Red } catch { Write-Host '*** -ErrorAction Stop caused this normally non-terminating error to be a terminating error. The try/catch block handled the error' -ForegroundColor Green }
  16.     Write-Host '*** Non-terminating error with try/catch, with -EA SilentlyContinue ***'
  17.     try { TestError -NonTerminating -ErrorAction SilentlyContinue; Write-Host '*** -ErrorAction SilentlyContinue caused the error message to be surpressed and the script continues as if nothing was wrong. The try/catch block had no affect' -ForegroundColor Green } catch { Write-Host '!!! caught non-terminating error' -ForegroundColor Red }
  18.     Write-Host '*** Non-terminating error with try/catch ***'
  19.     try { TestError -NonTerminating; Write-Host '*** No ErrorAction was specified, so the error was displayed. But since this was a non-terminating error the script continues anyway. The try/catch block had no affect' -ForegroundColor Green } catch { Write-Host '!!! caught non-terminating error' -ForegroundColor Red }
  20.     Write-Host '*** Non-terminating error w/o try/catch ***'
  21.     TestError -NonTerminating
  22.     Write-Host '*** No error handling at all, so the non-terminating error was displayed and the script continues anyway' -ForegroundColor Green
  23.     Write-Host '*** Non-terminating error w/o try/catch, with -EA SilentlyContinue ***'
  24.     TestError -NonTerminating -ErrorAction SilentlyContinue
  25.     Write-Host '*** The non-terminating error message was suppressed by -ErrorAction SilentlyContinue. Script execution continues' -ForegroundColor Green
  26. }
  27. & {
  28.     trap { Write-Host '*** This non-terminating error was changed to a terminating error by -ErrorAction Stop. Script would normally end here' -ForegroundColor Green; continue }
  29.     Write-Host '*** Non-terminating error w/o try/catch, with -EA Stop ***'
  30.     TestError -NonTerminating -ErrorAction Stop
  31.     # Write-Host '*** Continuing after Non-terminating error ***' -ForegroundColor Green
  32. }
  33. & {
  34.     trap { Write-Host '!!! Terminating error trapped. This should not happen' -ForegroundColor Red; break }
  35.     Write-Host "`n`r`n`r*** Terminating error with try/catch, with -EA Stop ***"
  36.     try { TestError -ErrorAction Stop; Write-Host '!!! Error not caught' -ForegroundColor Red } catch { Write-Host '*** -ErrorAction Stop was used but had no effect; a terminating error occurred anyway and was handled by the try/catch block' -ForegroundColor Green }
  37.     Write-Host '*** Terminating error with try/catch, with -EA SilentlyContinue ***'
  38.     try { TestError -ErrorAction SilentlyContinue; Write-Host '!!! Error not caught' -ForegroundColor Red } catch { Write-Host '*** -ErrorAction SilentlyContinue was used but had no effect; a terminating error occurred anyway and was handled by the try/catch block' -ForegroundColor Green }
  39.     Write-Host '*** Terminating error with try/catch ***'
  40.     try { TestError; Write-Host '!!! Error not caught' -ForegroundColor Red } catch { Write-Host "*** No -ErrorAction was used, but it wouldn''t have mattered anyway since this was a terminating error which was handled by the try/catch block" -ForegroundColor Green }
  41. }
  42. & {
  43.     trap { Write-Host '*** A terminating error occurred regardless of -ErrorAction. The script would normally have ended here since there was no surrounding try/catch block' -ForegroundColor Green; continue }
  44.     Write-Host '*** Terminating error w/o try/catch ***'
  45.     TestError
  46.     Write-Host '*** Terminating error w/o try/catch, with -EA SilentlyContinue ***'
  47.     TestError -ErrorAction SilentlyContinue
  48.     Write-Host '*** Terminating error w/o try/catch, with -EA Stop ***'
  49.     TestError -ErrorAction Stop
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement