Error Handling in PowerShell

A good practice while working on any script is to have a mechanism for error handling. Though it may take a little bit of additional time, its rewards are priceless. Before moving to Error handling in PowerShell, first go through what type of errors we may encounter.

Error Types


There are two types of errors that can be occurred during script execution. 

  1. Terminating 
  2. Non-terminating 

As the name implies, terminating error will stop the program from further execution, whereas a non-terminating error will not stop the execution. An example of terminating error would be a syntax error whereas an example of non-terminating error would be file/path not found.

Terminating errors can be caught and handled while Non-terminating errors cannot be caught by try-catch block.

Examples: 

Terminating error: Script execution halted when error occurred.


Non-terminating error: Write-Host "Still running" executed even after error occurred.

Convert Non-terminating errors into Terminating            

If you want to trap the error using try-catch block, hide it from user and provide some user friendly message. You have to convert non-terminating error into terminating.

To do that we have ErrorAction parameter. Every PowerShell cmdlet supports ErrorAction. By specifying -ErrorAction Stop on the end of a cmdlet you ensure that any errors it throws are treated as terminating and can be caught.



Change in execution flow is visible in snapshot. Once we used ErrorAction with stop, Non-terminating error converted to terminating one.

Possible values for ErrorAction parameter:
Continue - Logs error to $Error, displays error to console, continues execution.
Stop - Logs error to $Error, displays error to console, terminates.
SilentlyContinue - Logs error to $Error, does not display error, continues processing.
Ignore - Does not log error to $Error, does not display error, continues processing.

It is also possible to make all error as terminating for session by using $ErrorActionPreference = Stop as first line.

Error Handling via try-catch block


In PowerShell, the error handling is done through try and catch blocks. The try block will have the code, that may likely throw an error. The catch block contains the code or action to be executed in case of an error that is thrown by the try block. Additionally, a finally block can be used to free up the resources. Mostly, non-terminating errors can’t be handled in PowerShell. To handle such errors, they needed to be converted to a terminating error (Which we already discussed). 


When you have cmdlets in a try/catch and you want PowerShell to go to the catch, you still need to specify ErrorAction. Just because a cmdlet is in a try/catch does not mean PowerShell will go to the catch if the cmdlet encounters an error.


You can also create multiple catch blocks to handle specific type error


The exception type is checked for each catch block until one is found that matches your exception. Only one catch block is invoked even if there are multiple matches.


Adding proper exception handling to your scripts not only make them more stable, but also makes it easier for you to troubleshoot those exceptions.

Youtube Video link: 


Comments

Popular Posts

How to Import and Export Delimited Files, like CSV, in PowerShell

PowerShell Arithmetic Operators

How to generate a GUID in PowerShell