Error Handling in ASP.NET

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
Gopi
gbreddyAsked:
Who is Participating?
 
prairiedogCommented:
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.

Jeffrey
0
 
prairiedogCommented:
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"
        SmtpMail.Send(oEmail)
    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
            Response.Redirect("~/AccessDenied.aspx")
        End If

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

        Response.Redirect("~/YourCustomErrors.aspx")
    End Sub
0
 
gbreddyAuthor Commented:
Error handling is not for sending a mail
let me explain clearly

i have a

function A
{
   Try
    {
    ......
    ......
    }
   Finally
  {
   ........
   .......
   }
}

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
Gopi
0
 
gbreddyAuthor Commented:
Thanks alot :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.