Using a Try construct in Visual Basic .NET

Drawer Data Matrix in Visual Basic .NET Using a Try construct

22.7 Using a Try construct
Data Matrix Creator In Visual Basic .NET
Using Barcode drawer for .NET framework Control to generate, create Data Matrix image in .NET applications.
www.OnBarcode.com
Data Matrix ECC200 Recognizer In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
The Try construct eliminates the need to keep track of scopes, continuing, and so forth. It s a much simpler construct. Like Trap, it can be used to provide different actions for different exceptions. For these examples, I ll be sticking with the simplest form, which handles all exceptions the same way. If you want to see examples of a Try construct that handles different exceptions differently, run help about_try_catch in the shell.
Paint QR Code ISO/IEC18004 In VB.NET
Using Barcode printer for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications.
www.OnBarcode.com
PDF 417 Encoder In VB.NET
Using Barcode creator for VS .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications.
www.OnBarcode.com
Trapping and handling errors
Creating Code-128 In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code 128 image in VS .NET applications.
www.OnBarcode.com
Making UPC Symbol In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create GS1 - 12 image in .NET framework applications.
www.OnBarcode.com
The following listing shows the revised function, now using a Try instead of a Trap.
Matrix Barcode Creator In VB.NET
Using Barcode generation for Visual Studio .NET Control to generate, create 2D Barcode image in .NET framework applications.
www.OnBarcode.com
Print UPC Case Code In VB.NET
Using Barcode generator for .NET Control to generate, create UPC Case Code image in VS .NET applications.
www.OnBarcode.com
Listing 22.5 Using a Try construct instead of the more complicated Trap
Data Matrix 2d Barcode Recognizer In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Read ECC200 In C#.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
function Get-Stuff { BEGIN { del c:\errors.txt -ea SilentlyContinue } PROCESS { Try { Get-WmiObject Win32_BIOS -comp $_ -ea Stop } Catch { $_ | Out-File c:\errors.txt -append } } } 'Server-R2','Notonline','Localhost' | Get-Stuff
Encoding Barcode In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Quick Response Code Creator In Java
Using Barcode creation for Android Control to generate, create Denso QR Bar Code image in Android applications.
www.OnBarcode.com
The differences between listings 22.4 and 22.5 are all within the PROCESS script block. I start with the keyword Try, and like all constructs, it can contain one or more commands within curly braces. In those braces, I ve placed the command that I think might cause a problem. I m still using -EA Stop to ensure that any nonterminating errors become terminating exceptions. If an exception does occur, the shell will immediately jump to the Catch portion of the construct, and execute whatever commands are contained within its curly braces. There s no jumping out of scope like there was with Trap, and the shell will always resume execution immediately following the Catch portion. In this case, that brings it to the end of the PROCESS script block, so it can loop back up and repeat the PROCESS script block for its third iteration.
Barcode Recognizer In .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Create Data Matrix In Objective-C
Using Barcode generation for iPad Control to generate, create Data Matrix ECC200 image in iPad applications.
www.OnBarcode.com
TRY IT NOW Just a quick reminder that you should be typing these in, or
Encode Barcode In Java
Using Barcode maker for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Making Data Matrix In Visual Studio .NET
Using Barcode generation for Reporting Service Control to generate, create Data Matrix image in Reporting Service applications.
www.OnBarcode.com
downloading them from MoreLunches.com, and reviewing the results. Because some of these commands generate lengthy results, and because the results aren t as important as what s happening in the script, I ll continue to omit the script output and focus on the script itself. There s another option for using Try, which is to add a Finally portion. This portion executes whether there is an error or not. Here s a quick example:
Paint Barcode In None
Using Barcode creator for Microsoft Word Control to generate, create Barcode image in Word applications.
www.OnBarcode.com
PDF 417 Decoder In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Try { Get-WmiObject Win32_BIOS -comp $_ -ea Stop } Catch { $_ | Out-File c:\errors.txt -append } Finally { Write-Host "Command executed" }
Barcode Drawer In None
Using Barcode creator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
QR Drawer In None
Using Barcode creation for Font Control to generate, create QR Code 2d barcode image in Font applications.
www.OnBarcode.com
It s legal to have just two of these parts: you always have to have the Try portion, and you can have either a Catch, a Finally, or both a Catch and a Finally. As I mentioned
The -ErrorVariable parameter
earlier, you can also include multiple Catch blocks if you want to handle different exceptions in different ways. I also mentioned earlier that I like to use Trap constructs as a high-level catch for unanticipated errors, and stick with Try for specific errors on specific commands. That implies that you can use both Trap and Try together, and you can. Here is a quick example of that.
Listing 22.6 Using Trap and Try in the same script
trap { "Unexpected error!" | Out-File c:\errors.txt -append continue } function Get-Stuff { BEGIN { del c:\errors.txt -ea SilentlyContinue } PROCESS { Try { Get-WmiObject Win32_BIOS -comp $_ -ea Stop } Catch { $_ | Out-File c:\errors.txt -append } } } 'Server-R2','Notonline','Localhost' | Get-Stuff
Any errors in Get-WmiObject will be handled by the Try construct. Its Catch portion logs the failed computer name, which is a very specific action. In the event that some terminating exception occurs elsewhere, the shell will eventually find the Trap construct at the top of the script (even if it has to exit the function s scope to do so), and that will log a more generic error message. Right now, I think the only way the Trap would execute is if Get-WmiObject ran into a problem, and Out-File wasn t able to write to Errors.txt (perhaps because the file was marked as read only). In that case, the shell would have to execute the Trap construct but it would fail also, because it s trying to write to the same file! You might want to modify this script yourself to handle the top-level error in a different fashion.
Copyright © OnBarcode.com . All rights reserved.