Link to home
Create AccountLog in
C#

C#

--

Questions

--

Followers

Top Experts

Avatar of DallasWebCenter
DallasWebCenter

Application_Error - Being Called Twice?
I have been having difficulty in determining why my Application_Error event in global.asax is seemingly fired twice.

I've created a sample web application to demonstrate what happens. There is NO other code (or pages) beyond a default.aspx, error.aspx, and the global.asax.

Default.aspx has this:
-Very simple code to do nothing more than throw an error.

    protected void Page_Load(object sender, EventArgs e)
    {
        throw (new DivideByZeroException());
    }

Error.aspx has this:
-Grab the exception out of the Session object and display a message.

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ex = (Exception)Session["Exception"];
        something.Text = ex.Message;
    }

Global.asax has this:
-Get the last error, put it in session and then redirect to error page.

    void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError().GetBaseException();
        HttpContext.Current.Session["Exception"] = ex;
        Server.ClearError();
        Response.Redirect("~/error.aspx", true);
    }

When debugging all goes according to plan for a little while:

1. The exception is found and tossed to Application_Error.
2. Application_Error puts the exception into the session.
3. The ClearError is called (I have read that this is necessary in order to store things in the session in Application_Error. Is this true?).
4. Response.Redirect fires... and then...
5. Application_Error is called again, but a null reference exception is thrown when putting ex into the session (presumably because there isn't an error pushed into ex -- I suppose it was cleared by ClearError).

So the questions:
1. Why the second visit to Application_Error?
2. Is Server.ClearError really necessary?

The web application has literally no other code in it other than what you see above, so there shouldn't be anything else generating an error. I'm sure nothing else is generating an error because on the 2nd visit to Application_Error the exception is null.

Any thoughts?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of abelabel🇳🇱

This is from the top of my head and may or may not help you: the forced redirect causes an error to be thrown (this is normal and happens always). I thought it was one of those errors that wouldn't reach the default error handler, but apparently I am wrong...

Server.ClearError() should be called if you do not wish the error to be reported in the event viewer, and the user to see the default error screen (as specified in the web.config). By clearing it you say "hey, stop this, it is handled".

Avatar of abelabel🇳🇱

PS: from the Response.Redirect docs, it says:

"Redirect calls End which throws a ThreadAbortException exception upon completion."
which may be exactly what happens here. However, I'm surprised that the exc. object is null

Avatar of DallasWebCenterDallasWebCenter

ASKER

Hi Abel-
Thanks for the superquick comments.

How can I best push the user towards the error.aspx page... user server.transfer?


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of abelabel🇳🇱

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
C#

C#

--

Questions

--

Followers

Top Experts

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).