Try Catch Finally Block in PowerShell | PowerShell Tutorial

preview_player
Показать описание
Try, Catch, and Finally blocks in PowerShell

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.

Android App:

github

Blog:

LinkedIn

External Source:

#powershell #ps
Рекомендации по теме