February 2019
Intermediate to advanced
626 pages
15h 51m
English
An error might be re-thrown within a catch block. This technique can be useful if a try block performs a number of dependent steps in a sequence where one or more might fail.
Re-throwing an error raised by a script can be as simple as using throw in a catch block:
try {
'Statement1'
throw 'Statement2'
'Statement3'
} catch {
throw
}
ThrowTerminatingError might be used instead, depending on the desired behavior:
function Invoke-Something {
[CmdletBinding()]
param ( ) try {
'Statement1'
throw 'Statement2'
'Statement3'
} catch {
$pscmdlet.ThrowTerminatingError($_)
}
}
When an error is re-thrown in this manner, the second instance of the error (within the catch block) is not written to either Error or an error variable. In ...
Read now
Unlock full access