A while back, I wrote a post entitled Using Try and Catch with PowerShell’s Invoke-WebRequest as I explored handling errors with PowerShell. Since then, I’ve learned other ways it can be useful. Specifically, when handling errors that are thrown by other bits of code such as functions and cmdlets being called to perform modular units of work.

In the example below, I’ve written two simple functions: ThrowErrors and LoopErrors. The LoopErrors function executes the ThrowErrors function while incrementing a variable. This is common when iterating through an array of objects.
function ThrowErrors() { throw "This is error number $i" } function LoopErrors() { Process { $i = 0 while ($i -le 10) { ThrowErrors $i++ } } } LoopErrors
The first time that LoopErrors calls ThrowErrors, the entire script will halt execution and display the error below.
This is error number 0 At line:3 char:5 + throw "This is error number $i" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (This is error number 0:String) [], RuntimeException + FullyQualifiedErrorId : This is error number 0
This behavior may be undesirable. In some cases, I want to continue through the list of objects. Or I want to tell my script what to do when an error is encountered, rather than abruptly exiting. Using -ErrorAction: $Continue within LoopErrors will not bypass the halt encountered by throw.
By modifying the LoopErrors function to use a Try and Catch pairing, any errors thrown by other bits of code is put into the “penalty box” outlined in the Catch section. From there, the script is told what to do in the event that an error is encountered.
The example below simply passes the error information – which is available as the current pipeline value stored in the $_ variable – as a Write-Warning message.
function ThrowErrors() { throw "This is error number $i" } function LoopErrors() { Process { $i = 0 while ($i -le 10) { try { ThrowErrors } catch { Write-Warning -Message $_ } $i++ } } } LoopErrors
When ThrowErrors sends a throw, it is caught by the Catch statement in LoopErrors and written as a warning to the console. The loop continues to increment the counting variable and completes after reaching the number 10. This is because the Catch section does not tell the script to stop running. It just says to display a warning.
WARNING: This is error number 0 WARNING: This is error number 1 WARNING: This is error number 2 WARNING: This is error number 3 WARNING: This is error number 4 WARNING: This is error number 5 WARNING: This is error number 6 WARNING: This is error number 7 WARNING: This is error number 8 WARNING: This is error number 9 WARNING: This is error number 10
I wouldn’t do this blindly! There are reasons the error has occurred. You still want to understand and actually handle the error being encountered. Using this method, however, can offer some simple functionality to build a script capable of completing the intended tasks without being at the mercy of an external throw. 🙂