Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application_Error Called but there is no Exception

While debugging my MVC 4 application in VS2010 SP1, the Application_Error() handler was invoked but Server.GetLastError() was null.

protected void Application_Error()
{
    var exception = Server.GetLastError();
    // exception is null.  How is that possible?
}

MSDN states Application_Error is

called if an unhandled exception occurs anywhere in your ... application... You can get information about the most recent error from the GetLastError method.

The MSDN docs for Server.GetLastError() states

Return value: The previous exception that was thrown.

How can I possibly be in a state where Application_Error() was called but Server.GetLastError() returns null?

like image 748
Eric J. Avatar asked Jul 27 '12 06:07

Eric J.


1 Answers

Are you using custom error handling in your web.config? I believe that if the browser issues a redirect, the previous exception will be lost. Take a look at this answer and see if your situation is similar.

like image 158
Tommy Avatar answered Oct 21 '22 07:10

Tommy