0

event Page_Error(object sender, EventArgs e) doesn't handle expection. Exception is handling by Visual Studio.

I tried throw Exception by Page_Load or button but still isn't working. I can't search any information about this problem. In my book is show simple example with Page_Load, Page_Error, Button and according my book this should working. I guess that in Web.config is missing something.

    void Page_Error(object sender, EventArgs e)
    {
        Response.Clear();
        Exception ex = Server.GetLastError();
        Response.Write("Something not yet");
        Response.Write(ex.Message);
        Server.ClearError();
    }

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Load event fired");
        throw new Exception();
    }

I expect that throwed Exception will be handled by Page_Error, not by Visual Studio.

3 Answers 3

0

You can handle the errors on application (Application_Error) level in global.asax and forward the request to a custom error page.

This SO thread might be good to look at: Exception handling in ASP.NET Webforms

Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely more suitable method to catch Exception in ASP.NET but in this case I want know what is reason that event Page_Error doesn't handle Exception althouth all books or tutorials say that it should.
0

Try making your Page_Error method as protected:

protected void Page_Error(object sender, EventArgs e)
    {
        Response.Clear();
        Exception ex = Server.GetLastError();
        Response.Write("Something not yet");
        Response.Write(ex.Message);
        Server.ClearError();
    }

3 Comments

Still not working. VS is handling Exception, not method. Thanks for trying
What do you mean by Visual Studio is handling exception? Have you try putting in a breakpoint inside Page_Error handler?
Okey I've noticed how is working. VS is throwing statement of exception but if i step next line i will jump to event Page_Error. In my understanding VS shouldn't throw statement of exception. Event Page_Error should working as try/catch. Now Is working that exception is throwing of program and next program jump to page_error.
0

You were throwing a wrong type of exception. Throw ApplicationException instead of Exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.