Suggested Videos
Part 68 - Application state real time example
Part 69 - Exception handling
Part 70 - Error events
If there is an unhandled exception, by default, the generic yellow screen of death is displayed. Instead, custom error pages can be displayed. Custom error pages can be defined at 2 levels
1. Application Level - In the web.config file using "customErrors" element.
2. Page Level - In the Page directive, using "ErrorPage" attribute.
Page level custom error pages takes precedence over application level custom error pages.
Custom error pages provide the flexibility of displaying a specific page in response to one or more of the available HTTP status codes. For a list of all the available HTTP status please visit the following Article.
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
To specify the custom error pages at an application level, use customErrors element in web.config
<customErrors mode="On" defaultRedirect="DefaultErrorPage.aspx">
<error statusCode="401" redirect="UnauthorizedErrorPage.aspx" />
<error statusCode="404" redirect="PageNotFoundErrorPage.aspx" />
<error statusCode="500" redirect="InternalServerErrorPage.aspx" />
</customErrors>
The mode attribute determines when a custom error page is displayed over the yellow screen of death, exception page. Mode attribute can have On, Off, or RemoteOnly. Default is RemoteOnly.
On - Custom error pages are displayed both on local and remote machines
Off - Custom error pages are not displayed anywhere
RemoteOnly - Custom error pages are displayed on remote machines, and exception page on local machine
If the redirection is done in Application_Error() event handler in Global.asax, custom error pages will have no effect.
In your application, if you have to display specific custom error pages for specific http status codes, then use custom errors. If you just have one generic error page, then Global.asax can be used.
Please note that, the exception object needs to be retrieved, before the user is redirected to a custom error page. Because a custom error page is displayed through redirection, the context for the error is lost and Server.GetLastError returns nothing from the target custom error page.
Part 68 - Application state real time example
Part 69 - Exception handling
Part 70 - Error events
If there is an unhandled exception, by default, the generic yellow screen of death is displayed. Instead, custom error pages can be displayed. Custom error pages can be defined at 2 levels
1. Application Level - In the web.config file using "customErrors" element.
2. Page Level - In the Page directive, using "ErrorPage" attribute.
Page level custom error pages takes precedence over application level custom error pages.
Custom error pages provide the flexibility of displaying a specific page in response to one or more of the available HTTP status codes. For a list of all the available HTTP status please visit the following Article.
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
To specify the custom error pages at an application level, use customErrors element in web.config
<customErrors mode="On" defaultRedirect="DefaultErrorPage.aspx">
<error statusCode="401" redirect="UnauthorizedErrorPage.aspx" />
<error statusCode="404" redirect="PageNotFoundErrorPage.aspx" />
<error statusCode="500" redirect="InternalServerErrorPage.aspx" />
</customErrors>
The mode attribute determines when a custom error page is displayed over the yellow screen of death, exception page. Mode attribute can have On, Off, or RemoteOnly. Default is RemoteOnly.
On - Custom error pages are displayed both on local and remote machines
Off - Custom error pages are not displayed anywhere
RemoteOnly - Custom error pages are displayed on remote machines, and exception page on local machine
If the redirection is done in Application_Error() event handler in Global.asax, custom error pages will have no effect.
In your application, if you have to display specific custom error pages for specific http status codes, then use custom errors. If you just have one generic error page, then Global.asax can be used.
Please note that, the exception object needs to be retrieved, before the user is redirected to a custom error page. Because a custom error page is displayed through redirection, the context for the error is lost and Server.GetLastError returns nothing from the target custom error page.
hello Pragim,
ReplyDeleteWhen i am practicing your code of Exception Handling. I am using a Page_error Event as you did in your tutorial, but when your are running a page you will be redirected to Error.aspx page but in my case it will show me the error and when i press F10 then it will me take to that page . What is wrong with my code please let me know. Here i am attaching my code.
namespace Exception_Handling
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/countryy.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Server.ClearError();
Response.Redirect("Error.aspx");
}
}
}
Try to run with Ctrl + F5 you will get to the error page dude.
Deleteyou have to put these markes in front of the page that you want to move to it like this:
DeleteResponse.Redirect("/~Error.aspx").
Yes Venkat.
ReplyDeleteSame is happening in my code. I skipped using try-catch-finally so that error on page should be handled by Page_Error Event Handler but the same is not working in my code. Plz Help!!
I have found the answer. In web.config file, set the compilation debug=false and when you execute that particular page. Run it the option "Run without debugging" so that way you will see error will introduce at Page_Level and will propogate further if you skip Server.ClearError(). Thanks my Teacher sunanda for the same.
Delete