Lesson 2: Using Common Reference Types in VB.NET

Maker QR-Code in VB.NET Lesson 2: Using Common Reference Types

Lesson 2: Using Common Reference Types
Generating Quick Response Code In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
www.OnBarcode.com
QR-Code Scanner In VB.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
catch (System.UnauthorizedAccessException ex) { Console.WriteLine("You do not have sufficient permissions."); } catch (Exception ex) { Console.WriteLine("Error reading file: " + ex.Message); }
Bar Code Generation In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Barcode Recognizer In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Exception handling also supports a finally block. The finally block runs after the try block and any catch blocks have finished executing, whether or not an exception was thrown. Therefore, you should use a finally block to close any streams or clean up any other objects that might be left open if an exception occurs. The following code sample closes the StreamReader object whether or not an exception occurs:
Draw QR Code In C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code image in .NET framework applications.
www.OnBarcode.com
QR Generator In .NET
Using Barcode creation for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
' VB Dim sr As StreamReader = New StreamReader("text.txt") Try Console.WriteLine(sr.ReadToEnd) Catch ex As Exception ' If there are any problems reading the file, display an error message Console.WriteLine("Error reading file: " + ex.Message) Finally ' Close the StreamReader, whether or not an exception occurred sr.Close End Try // C# StreamReader sr = new StreamReader("text.txt"); try { Console.WriteLine(sr.ReadToEnd()); } catch (Exception ex) { // If there are any problems reading the file, display an error message Console.WriteLine("Error reading file: " + ex.Message); } finally { // Close the StreamReader, whether or not an exception occurred sr.Close(); }
QR-Code Printer In VS .NET
Using Barcode generation for .NET Control to generate, create QR Code image in .NET framework applications.
www.OnBarcode.com
Paint QR In VB.NET
Using Barcode generator for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
Notice that the StreamReader declaration was moved outside the try block in the preceding example. This is necessary because the finally block cannot access variables that are declared within the try block, which makes sense because depending on where an exception occurred, variable declarations within the try block might not yet have been executed. To catch exceptions that occur both during and after the StreamReader
Data Matrix 2d Barcode Creator In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create Data Matrix image in .NET applications.
www.OnBarcode.com
Matrix Barcode Generator In VB.NET
Using Barcode creation for VS .NET Control to generate, create Matrix Barcode image in .NET applications.
www.OnBarcode.com
1
Making Code 39 Full ASCII In VB.NET
Using Barcode generation for .NET Control to generate, create Code 39 Extended image in .NET framework applications.
www.OnBarcode.com
I-2/5 Generation In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create ANSI/AIM ITF 25 image in .NET framework applications.
www.OnBarcode.com
Framework Fundamentals
GS1 - 13 Printer In Objective-C
Using Barcode drawer for iPhone Control to generate, create EAN-13 image in iPhone applications.
www.OnBarcode.com
Barcode Encoder In Java
Using Barcode maker for BIRT reports Control to generate, create barcode image in BIRT applications.
www.OnBarcode.com
declaration, use nested try/catch/finally blocks. Typically, all code except for simple variable declarations should occur within try blocks. Exceptions that occur outside of try blocks, or exceptions that occur within a try block but do not have a catch block that matches their type, are passed up to the code that called the current method. If no higher-level code catches the exception, the exception is considered unhandled, and the CLR stops running the application. The Exception.Message property provides a text message that describes the exception. For example, if you attempt to open a file that does not exist, the .NET Framework throws an exception with the message, Could not find file 'filename'. While many users can interpret the exception messages (many of which are more complex than the example message here), you should strive to provide robust error handling and custom error messages for common scenarios. The Exception.StackTrace property is useful for debugging because it includes the specific file and line number that initiated the exception. While you should never show Exception.StackTrace to a user, you should log the data in an event log if you might need to troubleshoot the exception outside the debugging environment. The following shows a StackTrace generated when a file could not be found:
Paint UPC A In None
Using Barcode maker for Software Control to generate, create UPC-A Supplement 5 image in Software applications.
www.OnBarcode.com
GTIN - 12 Maker In .NET
Using Barcode encoder for Reporting Service Control to generate, create GTIN - 12 image in Reporting Service applications.
www.OnBarcode.com
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path) at ConsoleApplication2cs.Program.Main(String[] args) in C:\apps\MyApps\Program.cs:line 16
QR Code Printer In VS .NET
Using Barcode creation for Reporting Service Control to generate, create QR Code ISO/IEC18004 image in Reporting Service applications.
www.OnBarcode.com
Bar Code Encoder In None
Using Barcode printer for Software Control to generate, create bar code image in Software applications.
www.OnBarcode.com
NOTE
Scanning QR-Code In C#.NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Barcode Drawer In None
Using Barcode creator for Online Control to generate, create barcode image in Online applications.
www.OnBarcode.com
Robust error handling improves the user experience when problems occur and greatly simplifies debugging. However, exception handling does incur a slight performance penalty. To conserve space and focus on specific topics, sample code within this book typically will not include exception handling.
Lab: Working with Reference Types
The following exercises reinforce knowledge of reference types, strings, and exceptions. If you encounter a problem completing an exercise, the completed projects are available along with the sample files.
Lesson 2: Using Common Reference Types
Exercise 1: Identify Types as Value or Reference
In this exercise, you write a Console application that displays whether objects are value or reference types. 1. Using Visual Studio, create a new Console Application project. Name the project List-Value-Types. 2. In the Main method, create and initialize instances of the following classes: SByte Byte Int16 Int32 Int64 String Exception The following code demonstrates this:
' VB Dim a As SByte = 0 Dim b As Byte = 0 Dim c As Int16 = 0 Dim d As Int32 = 0 Dim e As Int64 = 0 Dim s As String = "" Dim ex As Exception = New Exception // C# SByte a = 0; Byte b = 0; Int16 c = 0; Int32 d = 0; Int64 e = 0; string s = ""; Exception ex = new Exception();
3. Add each of the instances to a new object array, as the following code demonstrates:
' VB Dim types As Object() = { a, b, c, d, e, s, ex } // C# Object[] types = { a, b, c, d, e, s, ex };
Copyright © OnBarcode.com . All rights reserved.