Sunday, April 11, 2010

Error Handling In Asp.Net By Overriding OnError Function

add the following code in your code behind to show the errors if there is any:
protected override void OnError(EventArgs e)
{
HttpContext context= HttpContext.Current;
Exception exception = context.Server.GetLastError();
string errorInfo =
"
Offending URL: " + ctx.Request.Url.ToString() +
"
Source: " + exception.Source.toString() +
"
Message: " + exception.Message.toString() +
"
Stack trace: " + exception.StackTrace.toString();
context.Response.Write(errorInfo);
// To let the page finish running we clear the error
context.Server.ClearError();
base.OnError(e);
}
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 = "
Offending URL: " + ctx.Request.Url.ToString() + "
Source: " + exception.Source + "
Message: " + exception.Message + "
Stack trace: " + exception.StackTrace;
ctx.Response.Write(errorInfo);
// -------------------------------------------------- // To let the page finish running we clear the error // -------------------------------------------------- ctx.Server.ClearError();
base.OnError(e); }

No comments:

Post a Comment