Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

Please give me an example of page level error handling
a)Using 'ErrorPage' attribute in '@page' directive and
b)Using 'protected override void OnError(EventArgs e)' method.

I tired an example which is mentioned below,
Step 1: In web.config,

  <customerrors mode="On">

Step 2: In my aspx page,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ErrorHandling.aspx.cs" Inherits="TutorialSite.ErrorHandling" ErrorPage="~/ErrorPage.aspx"%>

Step 3: In my aspx.cs page, I tired to navigate to the page 'Page200.aspx', which is not existing.
protected void Page_Load(object sender, EventArgs 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;


// --------------------------------------------------
// To let the page finish running we clear the error
// --------------------------------------------------


But OnError is not fired after an error and I got the default error page itself.

Updated 27-Feb-12 4:05am
ZurdoDev 27-Feb-12 8:52am
This sure sounds like a homework problem. Sorry, do the work yourself.
N!dh!sh 27-Feb-12 10:07am
This is not a homework yar. If you try page level error handling with an example, you will understand why I asked this question.
ZurdoDev 27-Feb-12 10:11am
If you want global error handling just add protected void Application_Error(object sender, EventArgs e)
{ } to Global.asax and in the future, please ask a more clear question.
[no name] 27-Feb-12 8:55am
What have you tried so far?

1 solution

To lazy to search for yourself? Looky here: How to: Handle Page-Level Errors[^]. As for the "ErrorPage" attribute, it's the same as setting the ErrorPage property by code: Page.ErrorPage Property[^].


N!dh!sh 27-Feb-12 9:57am
Hi Manfred,
I already tired the examples in the above link. But in practical both examples are not working. If you try them by yourself, you could notice them.
Manfred Rudolf Bihy 27-Feb-12 10:26am
Well, your code does explain it a bit. Response.Redirect makes a complete round trip to the browswer back to the server. It is as if the browser had made a new request, albeit to a page that does not exist. So there never is an error on the page you mentioned. To try out your scenario you'd have to make sure that an error occurred directly in your Page_Load method.

Please try this statement instead of the Response.Redirect: throw new Exception("Just for testing");

That should do the trick for you and cause a Page Error triggering your OnError handler.
Manfred Rudolf Bihy 27-Feb-12 10:29am
Instead of the throw statement you could also use Server.Transfer instead of Response.Redirect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900