Winnersh Triangle Web Solutions Limited View Cart

Timesaving tools for software developers

HOME | NEWS | PRODUCTS | DOWNLOADS | SPECIAL OFFERS | ORDERING | UPGRADES | CONTACT 
 
Search Website

PRODUCTS
The Website Utility
.NET Documentation Tool
ASP Documentation Tool
ASP.NET Documentation Tool
VB 6.0 Documentation Tool
PHP Documentation Tool
SQL Documentation Tool
MySQL Documentation Tool
JavaScript Banner Ad Rotator
Indexing Service Companion
HubPages Success Guide
Product Ordering
Special Offers

VB.NET Error Handling Using Try ... Catch Blocks


Error handling in Visual Basic 6.0 largely consists of On Error functionality such as On Error Resume Next and On Error Goto. While sufficient to create basic error handling, they can be fiddly to use, especially on blocks of code that the developer knows is quite likely to raise errors on occasions (such as a disk resource or a database connection being unavailable).

Even more basic is the error handling in classic ASP's VBScript, aside from an On Error Resume Next facility there was not much of any use in error handling, and even then the On Error Resume Next functionality did not always work reliably, especially when used within Functions.

Happily, the error handling capabilities of the .NET Framework's VB.NET programming language are greatly enhanced compared to Visual Basic 6.0 and VBScript.

Handling faulty VB.NET code

The following ASP.NET sample code loads a user control from a file and adds it to a PlaceHolder control:

Dim UserControlWeeklyOffers As Control = LoadControl("WeeklyOffers.ascx")
PlaceHolder1.Controls.Add(UserControlWeeklyOffers)

If the user control file WeeklyOffers.ascx does not exist, then the following ASP.NET error will be displayed in the web browser:

The file '/WeeklyOffers.ascx' does not exist.

However, if the code is enclosed within a Try ... Catch statement, then the page will be displayed in the web browser, but the user control will not be loaded into the PlaceHolder control.

Try
  Dim
UserControlWeeklyOffers As Control = LoadControl("WeeklyOffers.ascx")
  PlaceHolder1.Controls.Add(UserControlWeeklyOffers)
Catch
  Response.Write("Could not load weekly offers.")
End Try

In this example, if the WeeklyOffers.ascx file does not exist then the Could not load weekly offers message will be displayed instead.

Note that the Catch statement is not required, so the following code may also be used:

Getting full details of an Exception

The Try ... Catch block can be extended to give further details of the exception generated by the failing code. In the sample below an Exception object, Ex is instantiated if the code between the Try and Catch statements encounters an error:

Try
  Dim
UserControlWeeklyOffers As Control = LoadControl("WeeklyOffers.ascx")
  PlaceHolder1.Controls.Add(UserControlWeeklyOffers)
Catch Ex As Exception
  Response.Write("Could not load weekly offers. ")
  Response.Write("Due to: " & Ex.Message)
End Try

Using the Finally statement

The Finally statement can be used denote code that needs to run regardless of whether the code in the Try code block was successfully executed. In the example below, a Wednesday Offers control is loaded regardless of whether or not the Weekly Offers control was loaded:

Try
  Dim
UserControlWeeklyOffers As Control = LoadControl("WeeklyOffers.ascx")
  PlaceHolder1.Controls.Add(UserControlWeeklyOffers)
Catch Ex As Exception
  Response.Write("Could not load weekly offers. ")
  Response.Write("Due to: " & Ex.Message)
Finally
  If DateTime.Now.DayOfWeek = DayOfWeek.Wednesday Then
    Dim UserControlWednesdayOffers As Control = LoadControl("WednesdayOffers.ascx")
    PlaceHolder1.Controls.Add(UserControlWednesdayOffers)
  End If
End
Try

Logging/emailing errors

Once the VB.NET code has been modified to trap exceptions, it is feasible to write a .NET routine that will log errors or email them to a system administrator. This allows application developers to monitor application performance, detect possible security issues and allow them to respond quickly to sudden problems such as the unavailability of a database server for example.



© Copyright 2002 - 2011 Winnersh Triangle Web Solutions Limited. Registered company number: 4493816.       Sales Policy | Site Map