Wednesday, December 29, 2010

How to handle errors in C# (single page)

This section tells you how to handle an error on a single page:



protected override void OnError(EventArgs e)
{
  // At this point we have information about the error
  HttpContext ctx = HttpContext.Current;


  Exception exception = ctx.Server.GetLastError ();


  string errorInfo = 
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source + 
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;


 MessageBox(errorInfo);


  // --------------------------------------------------
  // To let the page finish running we clear the error
  // --------------------------------------------------
  ctx.Server.ClearError ();
  
  base.OnError (e);


 }

No comments:

Post a Comment