Error Handling in ASP.NET

Posted on 2006-05-04
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
    LVL 14

    Expert Comment

    LVL 22

    Expert Comment

    Ok, here is a way to do:

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

      Public Shared Sub SendErrorEmail(ByVal exError As Exception)
            Dim toAddress As String = ""
            Dim fromAddress As String = ""
            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

    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

    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

    Thanks alot :)

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    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.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now