vb.net qr code sample s HANDLING EXCEPTIONS in Visual Basic .NET

Printer Data Matrix in Visual Basic .NET s HANDLING EXCEPTIONS

CHAPTER 16 s HANDLING EXCEPTIONS
Data Matrix ECC200 Generator In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix ECC200 Decoder In VB.NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
'Create data reader Dim dr As SqlDataReader = cmd.ExecuteReader 'Access nonexistent column Dim str As String = dr.GetValue(20).ToString 'Close reader dr.Close() Catch ex As System.InvalidOperationException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message str += ControlChars.NewLine + ControlChars.NewLine str += "Stack Trace: " + ex.StackTrace MessageBox.Show(str, "Specific Exception") Catch ex As System.Data.SqlClient.SqlException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "Database Exception") Catch ex As Exception Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message MessageBox.Show(str, "non-Database Exception") Finally If conn.State = ConnectionState.Open Then MessageBox.Show("Closing the connection") conn.Close() End If End Try
Printing QR In Visual Basic .NET
Using Barcode printer for Visual Studio .NET Control to generate, create QR Code 2d barcode image in .NET applications.
www.OnBarcode.com
ECC200 Creation In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Data Matrix image in VS .NET applications.
www.OnBarcode.com
s Testing whether a connection is open before attempting to close it isn t actually necessary. The Tip
Painting GTIN - 12 In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create UPC Symbol image in .NET applications.
www.OnBarcode.com
1D Encoder In VB.NET
Using Barcode maker for .NET Control to generate, create Linear Barcode image in .NET applications.
www.OnBarcode.com
Close method doesn t throw any exceptions, and calling it multiple times on the same connection, even
Generating EAN13 In VB.NET
Using Barcode creation for .NET framework Control to generate, create EAN 13 image in .NET framework applications.
www.OnBarcode.com
Generate Standard 2 Of 5 In Visual Basic .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create 2/5 Standard image in .NET framework applications.
www.OnBarcode.com
if it s already closed, causes no errors.
ECC200 Creation In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create Data Matrix 2d barcode image in .NET applications.
www.OnBarcode.com
Creating ECC200 In Java
Using Barcode creator for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
3. Run the program by pressing Ctrl+F5. Click the ADO.NET Exception-2 button, and you ll see the message box in Figure 16-5. Click OK. When the Finally block message appears, click OK, and then close the window.
GS1 - 12 Printer In C#
Using Barcode encoder for VS .NET Control to generate, create UPC-A image in Visual Studio .NET applications.
www.OnBarcode.com
PDF417 Generation In None
Using Barcode printer for Online Control to generate, create PDF-417 2d barcode image in Online applications.
www.OnBarcode.com
CHAPTER 16 s HANDLING EXCEPTIONS
Painting GS1 128 In Objective-C
Using Barcode encoder for iPad Control to generate, create GS1-128 image in iPad applications.
www.OnBarcode.com
Universal Product Code Version A Creation In None
Using Barcode encoder for Online Control to generate, create UCC - 12 image in Online applications.
www.OnBarcode.com
Figure 16-5. Handling a specific ADO.NET exception 4. For a quick comparison, you ll now generate a SQL Server exception, an error that occurs within the database. Alter the name of the stored procedure in the code to a name that doesn t exist at all within the Northwind database. For example: cmd.CommandText = "sp_Select_No_Employees"; 5. Run the program by pressing Ctrl+F5. Click the ADO.NET Exception-2 button, and you ll see the message box in Figure 16-6. Click OK. When the Finally block message appears, click OK, and then close the window.
GTIN - 128 Drawer In None
Using Barcode creation for Software Control to generate, create EAN / UCC - 13 image in Software applications.
www.OnBarcode.com
Scan Code 128B In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Figure 16-6. Handling a specific ADO.NET exception
Code 39 Extended Creation In Java
Using Barcode printer for Java Control to generate, create Code 3/9 image in Java applications.
www.OnBarcode.com
Quick Response Code Maker In Visual C#
Using Barcode creation for .NET framework Control to generate, create QR image in VS .NET applications.
www.OnBarcode.com
How It Works
Draw 2D Barcode In Visual Studio .NET
Using Barcode creator for Visual Studio .NET Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Painting Barcode In .NET
Using Barcode encoder for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
First you create the data reader and try to access an invalid column: 'Create data reader Dim dr As SqlDataReader = cmd.ExecuteReader 'Access nonexistent column Dim str As String = dr.GetValue(20).ToString so an exception is thrown because column 20, the value of which you try to get, doesn t exist. You add a new Catch clause to handle this kind of ADO.NET error: Catch ex As System.InvalidOperationException Dim str As String str = "Source: " + ex.Source.ToString str += ControlChars.NewLine + "Exception Message: " + ex.Message str += ControlChars.NewLine + ControlChars.NewLine str += "Stack Trace: " + ex.StackTrace MessageBox.Show(str, "Specific Exception")
CHAPTER 16 s HANDLING EXCEPTIONS
When an exception of type System.InvalidOperationException is thrown, this Catch clause executes, displaying the source, message, and stack trace for the exception. Without this specific Catch clause, the generic Catch clause will handle the exception. (Try commenting out this Catch clause and reexecuting the code to see which Catch clause handles the exception.) Next, you run the program for a nonexistent stored procedure: 'Specify that a stored procedure is to be executed cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "sp_Select_No_Employees" You catch your (first) database exception with Catch ex As System.Data.SqlClient.SqlException leading into the next topic: handling exceptions thrown by the database manager.
Handling Database Exceptions
An exception of type System.Data.SqlClient.SqlException is thrown when SQL Server returns a warning or error. This class is derived from System.SystemException and is sealed so it can t be inherited, but it has several useful members that can be interrogated to obtain valuable information about the exception. An instance of SqlException is thrown whenever the .NET data provider for SQL Server encounters an error or warning from the database. Table 16-1 describes the properties of this class that provide information about the exception. Table 16-1. SqlException Properties
Property Name
Class
Description
Gets the severity level of the error returned from the SqlClient data provider. The severity level is a numeric code that s used to indicate the nature of the error. Levels 1 to 10 are informational errors; 11 to 16 are user-level errors; and 17 to 25 are software or hardware errors. At level 20 or greater, the connection is usually closed. Gets a collection of key-value pairs that contain user-defined information. Specifies the HRESULT of the error. Contains one or more SqlError objects that have detailed information about the exception. This is a collection that can be iterated through. Specifies the help file associated with this exception. Gets the exception instance that caused the current exception. Gets the line number within the Transact-SQL command batch or stored procedure that generated the exception. Defines the text describing the exception. Specifies the number that identifies the type of exception. Specifies the name of the stored procedure that generated the exception.
Copyright © OnBarcode.com . All rights reserved.