Browse DevX
Sign up for e-mail newsletters from DevX


Errors In Your ASP.NET Code? Don't Throw a Fit, Throw an Exception! : Page 3

Even the best designed applications need to properly manage errors—both the errors you can plan for and those you cannot. In this article, you'll learn error handling techniques in ASP.NET. Topics will range from handling common errors with the Try...Catch syntax to logging unhandled errors into the Windows Event Log.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Common Exception Types
There are a number of different exception types in the .NET Framework. Table 2 lists a few of the more common exception classes and their derived classes, if any.

Table 2: Common exception types

Exception Class


Derived Classes


Generated when at least one of the arguments passed to a method is not valid

ArgumentNullException ArgumentOutOfRangeException ComponentModel.InvalidEnumArgumentException DuplicateWaitObjectException


Generated by error occurring in an arithmetic, casting, or conversion process

DivideByZeroException NotFiniteNumberException OverflowException


Generated by an error using ADO.NET components

Data.ConstraintException Data.DeletedRowInaccessibleException Data.DuplicateNameException Data.InRowChangingEventException Data.InvalidConstraintException Data.InvalidExpressionException Data.MissingPrimaryKeyException Data.NoNullAlllowedException Data.ReadOnlyException Data.RowNotInTableException Data.StringTypingException Data.TypedDataSetGeneratorException Data.VersionNotFoundException


Generated by a warning or error returned by SQL Server



Generated by an attempt to access an element of an array using an index that is outside the bounds of the array



Generated by an invalid casting or explicit conversion



Generated when an I/O error occurs

IO.DirectoryNotFoundException IO.EndOfStreamException IO.FileLoadException IO.FileNotFoundException IO.PathTooLongException

The Try...Catch...Finally code block in Listing 1 attempts to calculate the date and time 25 years from the value provided in the StartDate variable.

'I hope StartDate contains date/time information. Dim StartDate As Object Dim EndDate As Date Dim intYearsToAdd As Integer = 25 Try EndDate = _ DateAdd("yyyy", intYearsToAdd, StartDate) Catch ex As System.ArgumentException 'At least one argument has an invalid value Catch ex As ArgumentOutOfRangeException ' EndDate is later than December 31, 9999 Catch ex As InvalidCastException ' StartDate is not a date/time value Catch ex As Exception ' Something unexpected happened Finally ' Last code to execute before leaving End Try

You can handle the exceptions that you might expect from the DateAdd function in the first three Catch blocks. Any unexpected exception can be handled in the last Catch block. Keep in mind that no matter what happens, the Finally block will always be the last code to execute before leaving this Try...Catch...Finally code block.

SQLException Exception
Since a large number of the applications being developed today work with databases, the example code in Listing 2 shows the SQLException exception being used. The code in Listing 2 demonstrates how to use the SQLException object.

In the above code, the two lines that are most likely to fail and are thus most in need of error handling are the mySQLConnection.Open() and mySQLCommand.ExecuteReader() methods.

Constructing Your Own Exceptions
In addition to using the built-in exception types, you can also build your own. All user-defined application exception classes are derived from ApplicationException, unlike the built-in common language runtime exception classes derived from SystemException.

Let's take a look at a user-defined Exception class.

Public Class NotEnoughPizzaException Inherits System.ApplicationException Public Sub New() End Sub Public Sub New(ByVal strMessage As String) MyBase.New(strMessage) End Sub End Class

As you can see, the new Exception class is based on System.ApplicationException. I added a second overloading New method in order to pass in to the exception what to do about the problem.

Try Throw New NotEnoughPizzaException(_ "Better order more") Catch ex As NotEnoughPizzaException Response.Write(ex.Message) End Try

The above code uses the Throw command, which manually throws an exception for the appropriate Catch statement to process.

Working with Unhandled Errors
So far you've used VB.NET to handle potential problems that may arise in the application. What about errors that pop up due to unforeseen circumstances and could not be planned for in your code, such as an invalid URL?

There are three places in ASP.NET that determine how unhandled errors are managed and responded to. Listed in the order in which they occur, the three places are:

  1. In the Page_Error procedure on the ASPX page itself
  2. In Application_Error procedure in the global.asax file
  3. In the customError section of the web.config file.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date