6

In Webform1.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    throw new Exception("test exception");
}

In the Global.asax.cs:

protected void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() is HttpUnhandledException)
        Server.Transfer("ErrUnknown.aspx");
}

But the Application_Error event handler never gets called. Instead I get a runtime error page.

What do I have to do have Application_Error being called after an exception is thrown?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ChrisPeeters
  • 153
  • 2
  • 13
  • What does your web.config say for the custom errors section? – Rich Apr 09 '11 at 09:33
  • Nothing. Are you referring to the customErrors section? – ChrisPeeters Apr 09 '11 at 09:49
  • Yes, also check that compilation Debug=True is [set](http://msdn.microsoft.com/en-us/library/e8z01xdh%28v=vs.80%29.aspx) – Rich Apr 09 '11 at 09:52
  • Nothing because I want the Exception to be handled in Application_Error. Any idea why it doesn't get fired? Maybe interesting to note: I am using a VS.NET WebApplication project, not a VS.NET WebSite-project. could that be the reason? – ChrisPeeters Apr 09 '11 at 09:52
  • 2
    I realise this is already solved but for anyone reading this, the Customerrors section and Debug=True should have no affect on this. Errors should go through to the OnError and the Global ASAX Error event regardless of these settings. Be aware if an exception is thrown inheriting ConfigurationException that won't go through the OnError or the Global ASAX error event as discussed here... http://stackoverflow.com/questions/25299325/asp-net-onerror-not-catching-configurationerrorsexception – Mick Aug 15 '14 at 03:39

2 Answers2

5

It looks fine and the Application_Error should being called.

Did you checked by Debugging your application?

Actually you are missing Server.ClearError() so the exception is being passed to asp.net but you should suppress it here because you are handling it by yourself.

protected void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() is HttpUnhandledException)
    {
        // suppressing the error so it should not pass to asp.net
        Server.ClearError();
        Server.Transfer("ErrUnknown.aspx");
    }
}
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • Hello. the debugger breaks in the Page_Load at the statement that throws the exception and doesn't continue to Application_Error. any suggestions? – ChrisPeeters Apr 09 '11 at 09:47
1

I found the problem.

Server.Transfer("ErrUnknown.aspx")

was the cause.

While trying to view 'ErrUnknown.aspx' directly in the browser I realized I had an error in that page. After correcting it Server.Transfer works

Was is misleading though is that the event doesn't get fired while debugging the application?

Anyway.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ChrisPeeters
  • 153
  • 2
  • 13