How do I implement a global exception handler?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Guest UserRank #1,422 · 1.2K reputationPosted May 13, 2011, 4:43 AM EDT
Andrew FensterRank #983 · 2K reputationPosted May 13, 2011, 1:30 PM EDT
ELMAH is for ASP.Net. Are you talking about an ASP.Net application, or something else?
In ASP.Net you can catch exceptions in the Global.asax file as follows. (Written in VB.Net)
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim ex As Exception = HttpContext.Current.Server.GetLastError()
'Code to log error goes here.
HttpContext.Current.Server.ClearError()
'Code to redirect user to error page goes here.
Catch ex As Exception
'DO NOTHING ... do not want to have infinite loop
End Try
End Sub
This code will catch and process all exceptions which weren't caught at some earlier stage.