Powershell Try Catch Finally for better error handling

Error handling is the most crucial part of any programming language. In windows Powershell scripts the exception handling will be carried out using try catch and finally blocks. 

Syntax Try {…} Catch {…} Finally {…}

Try Block

The try block contains the section of code which you need to monitor for errors. If any error occurs in the try block then the error is saved to $error variable and then Powershell  looks for catch block for catching specific error.

If the catch block is not found then Powershell looks at the parent level catch block or a trap statement to handle error.  If the script doesn’t have any catch block it goes to finally block. If the error cannot be handled then it is written to error stream.

Catch Block

Catch statement includes commands to handle the error.  A catch block can specify which error type it can catch. A try statement can have multiple catch blocks to handle different types of errors that occur.

Usually in the catch block we log the error message into a database or a file for later reference.

Finally Block

Finally block is used to free up resources. PowerShell runs the Finally block before the script terminates or before the current block goes out of scope.

Finally block statements will run regardless

  • Regardless of whether the Try block encounters a terminating error.
  • If CTRL+C is used to stop the script.
  • If an Exit keyword stops the script from within a Catch block.

Example of Try Catch Finally blocks in Powershell

Terminating and Non-Terminating Errors

Whenever an exception occurs in Powershell you will see a red text on the screen.  It is difficult to identify if the error is terminating or non terminating error. 

Terminating Errors

As the name indicates a terminating error is an error that will halt/stop the execution of the function or statements. 

Terminating error are refereed as exceptions and it occurs when you make a syntax error or run out of memory, that is a terminating error.

When the script encounters “NonsenseString”, it causes a terminating error. The Catch error block handles the error by running the statement list inside the block.

Non Terminating Error

Non-terminating errors allow Powershell to continue the script execution and the error usually occurs from cmdlets or other managed situations. Under normal circumstances they cannot be caught by Try-Catch-Finally blocks.

Treating Non-Terminating Errors as Terminating

So how do you catch a non terminating error? This can be achieved in Powershell using ErrorAction parameters. Basically you tell Powershell to treat it as terminating error. 

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. In our example above we are going to change our Get-Content line to:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.