Error Handling Implementation in VB.NET-MySQL Windows Applications – Do It Right and Same Time!

By Ernest Bonat, Ph.D.
President | Visual WWW, Inc.
Phone:503.730.4556 | Fax:503.715.4888
ebonat@evisualwww.com | www.evisualwww.com
Visual WWW is an Open Source Application
Development and Training Shop

Error handling implementation in business applications development is a must for any Application Developer today. Microsoft Visual Basic .NET (VB.NET) is the most common programming language for developing Windows applications. Many VB.NET codes are provided in many websites, materials and books without error handing implementation. Even in our forum I can see many VB.NET code and questions without error handling. My main idea for you in this paper is to show how to write error handling code in VB.NET 2005 or 2008. Let’s look at the error handling implementation in VB.NET 2002 and 2003 so we can compare them.

Structured Exception Handling implementation appears in Microsoft technologies for the first time in VS.NET 2002. This error handling was implemented by using Try…Catch…Finally statement in .NET Framework. The Try…Catch…Finally statement guaranties an easy way to handle all possible errors (exceptions) that may occur during application running. It contains three main blocks:

Try – contains the real programming code to be controlled for any possible error.

Catch – produces the error occurred during applications execution from Try block.

Finally – always executes last, regardless of whether the code in the Catch block has also executed. In general, this block is used for cleanup resources, like closing files and releasing created custom and unmanaged objects.

Below is the standard code of the Structured Exception Handling

‘Declaring and initializing variables
Try
‘Programming lines of code
Catch ex As MySqlException
‘Error handling code
Finally
‘Cleanup custom and unmanaged resources if necessary
End Try

So far, the main problem I found with Try and Finally blocks. Most developers fully understand very well the purpose of the Catch block. It’s very clear that the Try block must include the entire programming code of the applications. If any code is written outside the Try block and it generates an execution error the application crashes.

Let’s look at the following code below. As you can see the connection string property (ConnectionString) of the MySQL ADO.NET connection object MySqlConnectionObject has been hardcode outside the error handling structure. This is a bad programming practice because the connection string should not be hardcoded and set outside the error handling structure.

Dim MySqlConnectionObject = New MySqlConnection
MySqlConnectionObject.ConnectionString = “Server=???;user id=???;password=???;database=???;”
Try
MySqlConnectionObject.Open()
‘More code…
Catch ex As MySqlException
MsgBox(”An error occurred. ” & ex.Message)
Finally
MySqlConnectionObject.Close()
End Try

Based on the code below we can see that the connection string was stored in the application configuration file and it was set to the connection object inside the error handling structure. Just to mention the connection object has been disposed (closed) and destroyed property in the Finally block. Don’t destroy the connection object at the end of the Try block because if for any reason the application crashes before the connection object will not be destroyed, it’ll be floating on the server waiting for the Garbage Collector (GC) process. For more info about this important topic please read the paper “Define and Store MySQL ADO Connection String in VB.NET 2005” and download the source code.

Dim conn = New MySqlConnection
Dim MySQLConnectionString As String = My.Settings.MySQLConnectionString
Try
MySqlConnectionObject.ConnectionString = MySQLConnectionString
MySqlConnectionObject.Open()
More code…
Catch ex As MySqlException
MsgBox(”An error occurred. ” & ex.Message)
Finally
If Not IsNothing(MySqlConnectionObject) Then
MySqlConnectionObject.Dispose()
MySqlConnectionObject = Nothing
End If
End Try

In VB.NET 2005, Microsoft introduced for the first time the Using statement to dispose unmanaged recourses like file handle, COM wrapper, ADO.NET connection object, etc. Managed resources are disposed of by the .NET Framework GC without any extra coding on your part. Here is the main structure of the Using statement:

Using (Resource list | Resource expression)
‘Programming lines of code
End Using

Where:

Resource list - required if you do not supply resource expression. List one or more system resources that this Using block controls

Resource expression - required if you do not supply resource list. Reference variable or expression referring to a system resource to be controlled by this Using block statements

End Using - required. Terminates the definition of the Using block and disposes of all the resources that it controls

The program can handle an exception error that might occur within the Using statement by adding a complete Try…Catch statement as following:

Using (Resource list | Resource expression)
Try
‘Programming lines of code
Catch ex As MySqlException
‘Error handing code
End Try
End Using

As you can see the Finally block is not at all required because the End Using statement takes care of destroying unmanaged resources. This is very useful for any Application Developer today. The above VB.NET programming structure should the standard for database business applications development today.

If we apply this idea to the same connection object MySqlConnectionObject shown above, the general structure code will look likes:

Using MySqlConnection As New MySqlConnection(My.Settings.MySQLConnectionString)
Try
‘More code…
Catch ex As MySqlException
MsgBox(”An error occurred. ” & ex.Message)
End Try
End Using

At the point, as I explained before, the connection object does not need to be disposed and destroyed. As you can see he Using statement simplifies the VB.NET code and it will save hundreds of development hours.

As a conclusion I want to make sure that every one in this forum reads and understand the importance of the Using statement in database business applications development using MySQL 5.0 and VB.NET 2005 (2008). To learn more about it feel free to read the paper “Load and Search MySQL Data Using VB.NET 2005 in Windows Applications” (http://dev.mysql.com/tech-resources/articles/ebonat-load-and-search-mysql-data-using-vbnet-2005.html) and download the source code. Please feel free to post any ideas and/or questions about this topic

Happy Using statement code!

Leave a Reply