How to catch unhandled exceptions in a page

This will allow you to make sure all the errors encountered at the top level get logged.

private void Page_Error(object sender, System.EventArgs e)
Exception ep = Server.GetLastError();
LogFile.LogFatal(Util.FormatExceptionError("Page_Error","top level error", ep,4));
//Server.ClearError(); //we don't clear, but let the system produce message

To have the above run, it must be registered as a callback

private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);
this.Error += new System.EventHandler(this.Page_Error);


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: