Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function TestError {
- [cmdletbinding()]
- param([switch]$NonTerminating)
- # Write-Host 'Entering function' -ForegroundColor Yellow
- if ($NonTerminating) {
- Write-Error 'Non-terminating error; function execution continues'
- } else {
- throw [System.IO.IOException]'Terminating error; function execution ends'
- }
- }
- cls
- & {
- trap { Write-Host '!!! Non-terminating error trapped. This should not happen' -ForegroundColor Red; break }
- Write-Host '*** Non-terminating error with try/catch, with -EA Stop ***'
- 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 }
- Write-Host '*** Non-terminating error with try/catch, with -EA SilentlyContinue ***'
- 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 }
- Write-Host '*** Non-terminating error with try/catch ***'
- 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 }
- Write-Host '*** Non-terminating error w/o try/catch ***'
- TestError -NonTerminating
- Write-Host '*** No error handling at all, so the non-terminating error was displayed and the script continues anyway' -ForegroundColor Green
- Write-Host '*** Non-terminating error w/o try/catch, with -EA SilentlyContinue ***'
- TestError -NonTerminating -ErrorAction SilentlyContinue
- Write-Host '*** The non-terminating error message was suppressed by -ErrorAction SilentlyContinue. Script execution continues' -ForegroundColor Green
- }
- & {
- trap { Write-Host '*** This non-terminating error was changed to a terminating error by -ErrorAction Stop. Script would normally end here' -ForegroundColor Green; continue }
- Write-Host '*** Non-terminating error w/o try/catch, with -EA Stop ***'
- TestError -NonTerminating -ErrorAction Stop
- # Write-Host '*** Continuing after Non-terminating error ***' -ForegroundColor Green
- }
- & {
- trap { Write-Host '!!! Terminating error trapped. This should not happen' -ForegroundColor Red; break }
- Write-Host "`n`r`n`r*** Terminating error with try/catch, with -EA Stop ***"
- 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 }
- Write-Host '*** Terminating error with try/catch, with -EA SilentlyContinue ***'
- 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 }
- Write-Host '*** Terminating error with try/catch ***'
- 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 }
- }
- & {
- 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 }
- Write-Host '*** Terminating error w/o try/catch ***'
- TestError
- Write-Host '*** Terminating error w/o try/catch, with -EA SilentlyContinue ***'
- TestError -ErrorAction SilentlyContinue
- Write-Host '*** Terminating error w/o try/catch, with -EA Stop ***'
- TestError -ErrorAction Stop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement