Error Handling in ASP.NET

Posted on 2006-05-04
Medium Priority
Last Modified: 2013-11-25
Hi there,

I want to know how can we handle exceptions in ASP.NET?
I know that we can use TRY CATCH FINALLY but

if i have 20 pages in my ASP.NET application i want to write a common exception handler
what ios the procedure?
any sample code is appreciated

With Regards
Question by:gbreddy
  • 2
  • 2
LVL 14

Expert Comment

ID: 16604468
LVL 22

Expert Comment

ID: 16607160
Ok, here is a way to do:

First, create a clss (ie. ErrorHandler.vb), and add a shared public function (VB.net) in that class to send email

  Public Shared Sub SendErrorEmail(ByVal exError As Exception)
        Dim toAddress As String = "developer@yourdomain.com"
        Dim fromAddress As String = "from@yourdomain.com"
        Dim subject As String = "Application Error"
        Dim sbBody As New StringBuilder
        Dim body As String

        While Not (exError Is Nothing)
            'Format your email body in this block
            'For example: sbBody.append(........)

           'Then check if there is any inner exception
            exError = exError.InnerException
        End While

        body = sbBody.ToString()
        Dim oEmail as new System.Web.Mail.MailMessage
        oEmail.To = toAddress
        oEmail.From = fromAddress
        oEmail.Subject = subject
        oEmail.Body = body
        oEmail.BodyFormat = BodyFormat.Html
        SmtpMail.SmtpServer = "your smtp server address"
    End Sub

Then, in your global.asax, there is a sub called Application_Error, modify the sub like below:
  Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        If TypeOf Me.Server.GetLastError Is HttpRequestValidationException Then
        End If

        If Not (Me.Server.GetLastError.InnerException Is Nothing) Then
        End If

    End Sub

Author Comment

ID: 16614245
Error handling is not for sending a mail
let me explain clearly

i have a

function A

function B
  call A();


in this case if ERROR arise in function A there is no catch and it should be catch some where
we can write try catch in function B but with out this how can we catch the error

i think APPLICATION ERROR handling is useful but can anybody p[lease explain that
and sample code will be appreciated
with regards
LVL 22

Accepted Solution

prairiedog earned 600 total points
ID: 16614535
You argument is valid, but all error handlings have the same problem as you mentioned. For example, you can catch errors and save them in your database, then what if error occurred during saving data? Even you catch the errors, then what? Save it again in database? It will go to an infinite loop, right? Same as you write errors into event log, what if errors occurred during writing process? So I guess you can only wish that when you send email, or save in database, or write in event log, everything is fine.

The sample code in my previous post is from one of my projects, and it is working very well. APPLICATIOn_ERROR sub is an event handler that is fired whenever an error occurred in your application, that is why you can use it to handle your exception errors. Sample code? I think I have already included in my previous post.


Author Comment

ID: 16615234
Thanks alot :)

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question