February 2019
Intermediate to advanced
626 pages
15h 51m
English
trap is declared in a similar manner to the catch block:
trap { <script> }
trap [ExceptionType] { <script> }
trap [ExceptionType1], [ExceptionType2] { <script> }
A script may contain more than one trap statement, for example:
trap [InvalidOperationException] {
Write-Host 'An invalid operation'
}
trap {
Write-Host 'Catch all other exceptions'
}
The ordering of the preceding trap statements doesn't matter; the statement with the most specific error type is used to handle a given error.
PowerShell, as a script-based language normally executes statements in the order written. However, when using a script, function, or script block, the trap statement can appear anywhere; trap doesn't have to appear before the code it acts against. ...
Read now
Unlock full access