Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling

Status
Not open for further replies.

JasonNevin

Programmer
May 26, 2005
29
GB
I have nearly finished developing my first ASP.NET application using VB.NET. The bit I'm now struggling with is finding a good way of handling errors. What I would like is a single procedure that handles all errors from the application. i.e I'd like to do the following ;

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dim strStage as string

Try
strStage = "Opening database"
...code...
Catch ex as Exception
HandleError(ex,strStage)
end try

I'd also like any called procedures to pass errors up to this level.

Is this possible? Is it good practice?

Any suggestions would be welcome. I've trawled the web but can only seem to find examples of error handling that is carried out in each procedure. Seems inefficient to me.

Thanks.
 
Here's an example of global exception handling which I originally found at Using this method you will to receive an e-mail containg the exception details and direct the user to a "user-friendly" exception page whenever a non-http exception occurs.

Within the Global.asax.vb file, create a sub as follows...

Code:
' This example was created by [URL unfurl="true"]www.learnvisualbasic.net[/URL]
Imports System.Web.Mail

Sub CatchExceptions(ByVal sender as object, ByVal e as EventArgs)

Dim ex as System.Exception = Server.GetLastError()

If Not ex.GetType.FullName = "System.Web.HttpException" Then 
Dim Message as MailMessage = New MailMessage()

With Message
.From = "someone@somewhere.com"
.To = "YourEmailAddress@somewhere.com"
.Subject = "Error : " & ex.message
.Body = "InnerException: " & ex.InnerExceptionMessage & "StackTrace: " & ex.StackTrace & ControlChars.NewLine & "Date/Time: " Now.ToString & ControlChars.NewLine & "Source: " & Context.Current.Request.Url.ToString()
End With

SmtpMail.Send(message)


--
Not until I became a Network Administrator did the error message "See your Network Administrator for assistance" become petrifying.
 
I forgot the logic needed to re-direct the user. It is similar to follows, as also shown at
Do something similar the following within web.config file...
<customErrors> defaultRedirect="errorpage.aspx"
<error statusCode="404" redirect="404.html"
</customErrors>




--
Not until I became a Network Administrator did the error message "See your Network Administrator for assistance" become petrifying.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top