Try/Catch/Finally
When you use Try/Catch/Finally, the command to be executed is placed in the block Try. If an error occurs in the command, it will be recorded in the variable $Error, and the script execution will proceed to block Catch.
The script TestTryCatchFinally.ps1 use the command Try in an attempt to create the object. Object creation is in the variable $ob1. Cmdlet New-Object constructs an object. After creating the object and placing it in the variable $a you can view the members of an object using the cmdlet Get-Member. The following code illustrates this:
the
Use the unit Catch to catch the error that has occurred in the Try. You can specify the type of errors to capture, as well as the action that would happen when an error occurs. In the script TestTryCatchFinally.ps1 I follow the error of type System.Exception. Class System.Exeption .Net Framework is a base class from which all other exceptions (exceptions). This means that the System.Exeption is a universal class, in fact you can get all the predefined exceptions, both General and systematic. When catching errors, you can select what code you wish to execute. In this example, I output a simple string, which indicates that the script caught a system exception. Block Catch follows:
the
Block Finally sequences Try/Catch/Finally is always executed, regardless of whether an error occurred or not. This means that any things to complete, you want to make, for example, to explicitly release the COM object should be placed in the block Finally. In the script TestTryCatchFinally.ps1 block Finally illustrates the status that the script completed. Follows:
the
The entire script is TestTryCatchFinally.ps1:
TestTryCatchFinally.ps1
the
During the execution of the script TestTryCatchFinally.ps1 when the variable $ob1 is assigned the value "kenobie" error occurs because there is no object with the name "kenobie" that could be created by using the New-Object. The following picture shows the output of the script.

As you can see from the previous picture, the string "Begin Test" is displayed because it is outside the loop Try/Catch/Finally. Inside the unit Try string “Attempting to create new object kenobie” is because is performed before the command New-Object. This demonstrates that the unit Try always tries to run the code inside it. Members of "kenobie" not displayed, is not displayed as the string "new object kenobie created". This indicates that after the error occurs, the script proceeds to the next block. In the Catch is to capture the error type System.Exeption and displays a string "caught a system exception". The script then proceeds to block Finally and prints the string "end of script".
If the script variable $ob1 will be assigned the value "system.object" (which is the correct value), the unit Try will succeed completely. This can be seen in the following figure, members of the objects withdrawn, and the line indicates that the object is successfully created is also displayed. Block Catch does not work, and the string "end of script" from the block Finally displayed.

You can use multiple blocks of Catch in the Try/Catch/Finally. You need to keep in mind that when an exception occurs, Windows Powershell leaves the unit Try looking for Catch. Will be used the first unit of Catch that satisfies the conditions of the exception. Therefore, you must use the most specific exceptions first, turning to more General.
TestTryMultipleCatchFinally.ps1
the
The following figure shows the output of the script TestTryMultipleCatchFinally.ps1. Were made two changes: Commented out the commands $ErrorActionPreference and Get-Content foo. Thus generated error will occur when trying to create a nonexistent object. To find the specific error, I explored the variable $error after you start the script TestTryMultipleCatchFinally.ps1. Error is specified in Exception.
the

If the script has multiple errors, and the value of the variable $ErroActionPreference is set to "stop", the first error will cause a failure scenario. If you remove the comments from the teams $ErrorActionPreference and Get-Content, the first error will be caught by the unit System.Exception Catch and therefore will skip the argument exception. This can be seen in the following figure:

In General, try to transfer statue published in the unit ed Wilson. I hope to be someone useful.
Article based on information from habrahabr.ru
The script TestTryCatchFinally.ps1 use the command Try in an attempt to create the object. Object creation is in the variable $ob1. Cmdlet New-Object constructs an object. After creating the object and placing it in the variable $a you can view the members of an object using the cmdlet Get-Member. The following code illustrates this:
the
Try
{
"Attempting to create new object $ob1"
$a = new-object $ob1
"Members of the $ob1"
"New object $ob1 created"
$a | Get-Member
}
Use the unit Catch to catch the error that has occurred in the Try. You can specify the type of errors to capture, as well as the action that would happen when an error occurs. In the script TestTryCatchFinally.ps1 I follow the error of type System.Exception. Class System.Exeption .Net Framework is a base class from which all other exceptions (exceptions). This means that the System.Exeption is a universal class, in fact you can get all the predefined exceptions, both General and systematic. When catching errors, you can select what code you wish to execute. In this example, I output a simple string, which indicates that the script caught a system exception. Block Catch follows:
the
Catch
{
[system.exception]
"caught a system exception"
}
Block Finally sequences Try/Catch/Finally is always executed, regardless of whether an error occurred or not. This means that any things to complete, you want to make, for example, to explicitly release the COM object should be placed in the block Finally. In the script TestTryCatchFinally.ps1 block Finally illustrates the status that the script completed. Follows:
the
Finally
{
"end of script"
}
The entire script is TestTryCatchFinally.ps1:
TestTryCatchFinally.ps1
the
$ob1 = "kenobie"
"Begin test"
Try
{
"Attempting to create new object $ob1"
$a = new-object $ob1
"Members of the $ob1"
"New object $ob1 created"
$a | Get-Member
}
Catch [system.exception]
{
"caught a system exception"
}
Finally
{
"end of script"
}
During the execution of the script TestTryCatchFinally.ps1 when the variable $ob1 is assigned the value "kenobie" error occurs because there is no object with the name "kenobie" that could be created by using the New-Object. The following picture shows the output of the script.

As you can see from the previous picture, the string "Begin Test" is displayed because it is outside the loop Try/Catch/Finally. Inside the unit Try string “Attempting to create new object kenobie” is because is performed before the command New-Object. This demonstrates that the unit Try always tries to run the code inside it. Members of "kenobie" not displayed, is not displayed as the string "new object kenobie created". This indicates that after the error occurs, the script proceeds to the next block. In the Catch is to capture the error type System.Exeption and displays a string "caught a system exception". The script then proceeds to block Finally and prints the string "end of script".
If the script variable $ob1 will be assigned the value "system.object" (which is the correct value), the unit Try will succeed completely. This can be seen in the following figure, members of the objects withdrawn, and the line indicates that the object is successfully created is also displayed. Block Catch does not work, and the string "end of script" from the block Finally displayed.

You can use multiple blocks of Catch in the Try/Catch/Finally. You need to keep in mind that when an exception occurs, Windows Powershell leaves the unit Try looking for Catch. Will be used the first unit of Catch that satisfies the conditions of the exception. Therefore, you must use the most specific exceptions first, turning to more General.
TestTryMultipleCatchFinally.ps1
the
$ob1 = "foo"
"Begin test"
$ErrorActionPreference = "stop"
Try
{
Get-Content foo
"Attempting to create new object $ob1"
$a = new-object $ob1
"Members of the $ob1"
"New object $ob1 created"
$a | Get-Member
}
Catch [System.Management.Automation.PSArgumentException]
{
"invalid object"
}
Catch [system.exception]
{
"caught a system exception"
}
Finally
{
"end of script"
}
The following figure shows the output of the script TestTryMultipleCatchFinally.ps1. Were made two changes: Commented out the commands $ErrorActionPreference and Get-Content foo. Thus generated error will occur when trying to create a nonexistent object. To find the specific error, I explored the variable $error after you start the script TestTryMultipleCatchFinally.ps1. Error is specified in Exception.
the
PS C:\> $error | fl * -F
PSMessageDetails :
Exception : System.Management.Automation.PSArgumentException: Cannot find type
[foo]: verify that the assembly containing this type is loaded.
at System.Management.Automation.MshCommandRuntime.ThrowTerminat
ingError(ErrorRecord errorRecord)
TargetObject :
CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, C:\Users\dyak\SkyDrive\Scripts\habrahabr\TestTry
MultipleCatchFinally.ps1: line 10
PipelineIterationInfo : {}

If the script has multiple errors, and the value of the variable $ErroActionPreference is set to "stop", the first error will cause a failure scenario. If you remove the comments from the teams $ErrorActionPreference and Get-Content, the first error will be caught by the unit System.Exception Catch and therefore will skip the argument exception. This can be seen in the following figure:

In General, try to transfer statue published in the unit ed Wilson. I hope to be someone useful.
Комментарии
Отправить комментарий