Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts. The Trap statement can also be used to handle terminating errors in scripts.
A terminating error stops a statement from running. If PowerShell does not handle a terminating error in some way, PowerShell also stops running the function or script using the current pipeline.
Use the Try block to define a section of a script in which you want PowerShell to monitor for errors. When an error occurs within the Try block, the error is first saved to the $Error automatic variable. PowerShell then searches for a Catch block to handle the error. If the Try statement does not have a matching Catch block, PowerShell continues to search for an appropriate Catch block or Trap statement in the parent scopes. After a Catch block is completed or if no appropriate Catch block or Trap statement is found, the Finally block is run. If the error cannot be handled, the error is written to the error stream.
A Catch block can include commands for tracking the error or for recovering the expected flow of the script. A Catch block can specify which error types it catches. A Try statement can include multiple Catch blocks for different kinds of errors.
A Finally block can be used to free any resources that are no longer needed by your script.
try {<statement list>} catch [[<error type>][',' <error type>]*] {<statement list>} finally {<statement list>}
The following sample script shows a Try block with a Catch block:
When the script encounters "NonsenseString", it causes a terminating error. The Catch block handles the error by running the statement list inside the block.
PS C:\WINDOWS\system32> try { NonsenseString } catch { "An error occurred." } An error occurred.
A Try statement can have any number of Catch blocks. For example, the following script has a Try block that downloads MyDoc.doc, and it contains two Catch blocks
try { $wc = new-object System.Net.WebClient $wc.DownloadFile("http://www.contoso.com/MyDoc.doc","c:\temp\MyDoc.doc") } catch [System.Net.WebException],[System.IO.IOException] { "Unable to download MyDoc.doc from http://www.contoso.com." } catch { "An error occurred that could not be resolved." }
Within a Catch block, the current error can be accessed using $_, which is also known as $PSItem. The object is of type ErrorRecord
try { NonsenseString } catch { Write-Host "An error occurred:" Write-Host $_ } An Error occurred: The term 'NonsenseString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
To free resources used by a script, add a Finally block after the Try and Catch blocks. The Finally block statements run regardless of whether the Try block encounters a terminating error. PowerShell runs the Finally block before the script terminates or before the current block goes out of scope.
A Finally block runs even if you use CTRL+C to stop the script. A Finally block also runs if an Exit keyword stops the script from within a Catch block.