Understanding Error handling in Powershell (2021)

When it comes to writing code, error management is a given. Expected behavior can frequently be checked and validated. We use exception handling when anything unexpected happens. You can easily handle exceptions thrown by other people’s code, or you can create your own for others to handle.

What is Exception in PowerShell ?

An exception is a type of event that is thrown when standard error handling fails to resolve the problem. Attempting to divide a number by zero or running out of memory are both examples of exceptions. When specific problems arise, the creator of the code you’re using may create exceptions.

Throw, Try and Catch in PowerShell

When an exception occurs, we refer to it as a thrown exception. You must catch a thrown exception in order to manage it. The script will cease operating if an exception is thrown that isn’t captured by something.

Similarly, we have Try where we can put any logic and use try to catch the exception. Below are some examples of throw, try and catch respectively.

We throw an exception with the throw keyword to generate our own exception event.

				
					function hi
 {
  throw "hi all"
 }
 
				
			

This throws a runtime exception, which is a fatal error. A catch in a calling function handles it, or the script leaves with a notice like this.

				
					 hi

hi all
At line:3 char:1
+ throw "hi all"
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (hi all:String) [], RuntimeException
    + FullyQualifiedErrorId : hi all
				
			

In PowerShell (and many other languages), exception handling works by first trying a portion of code and then catching it if it throws an error. Here’s a sampling of what I’m talking about.

				
					function hi
{
throw "hi all"

 }
 try hi
{
   
catch
{
    Write-Output "Something threw an exception"
}

##Output:

Something threw an exception
				
			

Finally block in PowerShell

You don’t always need to handle an error, but you do need some code to run whether or not an exception occurs. This is exactly what a finally block does. The code inside finally will eventually get executed.

				
					function hi
{
throw "hi all"

 }

 try
{
    hi
}

catch
{
    Write-Output "Something threw an exception"
}

Finally

{
"PowershellGuru"
}

## Output ##

Something threw an exception
PowershellGuru
				
			

Cmdlet -ErrorAction

When you use the -ErrorAction Stop option with any advanced function or cmdlet, it converts all Write-Error statements into terminating errors that stop execution or can be caught.

Similarly, we have -ErrorAction SilentlyContinue which doesn’t display the error if any is there and continues without any break.

Let me show you how I use these in a script.

				
					$ErrorActionPreference = "Stop"

script-start
'
'
'
script-end


				
			

Instead of “Stop” we can use “SilentlyContinue”. The perks of using like this are we don’t have to mention it on every line where it is required.

Conclusion

In this post we have covered what is exception, try/catch/throw/finally with examples. Also, we have seen -erroraction cmdlets and how to actually use them. It must have been known now how the exception handling in Powershell is important. Let’s meet in our next post.

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!