Page_Error:
The Page_Error event handler provides a way to trap errors that occur at the page level. You can simply display error information or you can log the event or perform some other action.
void Page_Load(object sender, System.EventArgs e)
{
throw(new Exception());
}
public void Page_Error(object sender,EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
Server.ClearError();
}
Server.ClearError prevents the error from continuing to the Application_Error event handler.
Application_Error:
The Application_Error event handler is specified in the Global.asax file of application. It means other pages in same virtual directory can also use the same error handler.
void Page_Load(object sender, System.EventArgs e)
{
throw(new Exception());
}
//In Global.asax
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
Server.ClearError();
}
Note:
1) In both of the above methods AutoEventWireup in aspx page should be true. It is ASP attribute which wires up certain event handlers (like Page_Load, Page_Error etc) to the page.If this attribute is set to false then none of these events will fire. These event handler have fixed name which can't be changed.
2) The error details can be found in Server.GetLastError().GetBaseException().
Web.Config method:
If you do not call Server.ClearError or trap the error in the Page_Error or Application_Error event handler, the error is handled based on the settings in the
-> Off: Users receive the exception information and are not redirected to the defaultRedirect page. This mode is used mainly in development.
-> RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information. All other users are redirected to the defaultRedirect page. This mode is used mainly for debugging.
ResponseRewrite: Specifies that the URL to direct the browser to must be the original Web request URL.In simple words in this case the url which you see in webbrowser will be the application url only, but the http content will be of error page.Here server rewrite the html content of your application page.
The different among 2 can be seen on refresh of page. In 1st case error page will get refresh. but in 2nd Case your application will again try to reload and if again some error occurs then we will get error page again.
No comments:
Post a Comment