Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application_Error not firing

Status
Not open for further replies.

NeilTrain

Programmer
May 27, 2003
275
US
This one has been bugging me all day. Usually I try to catch as many errors as possible in each page. However because I have an upload form, and if the file uploaded is larger than the server limit (4MB by default) it will cause an application error, the page never fires. So I cant test for size in the page itself. So I googled a solution, to which I was told to catch the error in Application_Error in my global.asax page, then clear the error and redirect back to the upload form to show an error message.

Well as far as I can tell, the Application_Error is not getting fired at all. If I put bad code in there, I get a compile error, so at least the server is trying to compile it. But if I strip it down to just this, i get nothing
Code:
void Application_Error(object sender, EventArgs e)
{
     throw new Excpeption("got here");
}
but instead I still just get the same error page I was getting before:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Maximum request length exceeded.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Maximum request length exceeded.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): Maximum request length exceeded.]
System.Web.HttpRequest.GetEntireRawContent() +3315874
System.Web.HttpRequest.GetMultipartContent() +56
System.Web.HttpRequest.FillInFormCollection() +244
System.Web.HttpRequest.get_Form() +65
System.Web.HttpRequest.get_HasForm() +57
System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) +2070529
System.Web.UI.Page.DeterminePostBackMode() +63
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +133
 
ok I guess it was firing all along, I just wasnt using it right.

Apparently you can't throw a new error in the Application_Error method. (Well you CAN, but it gets ignored, the original error is the one that shows up in the event log) I was assuming it would override the error that got it there, and show my thrown exception.

Instead I tried just the following line to test if it fired:

Response.Redirect("
and sure enough, my file upload error sent me to a Google search for blah.

I am now testing for error codes, and if the one I am trying to catch comes up, i just redirect back to my file upload page with a ?error=FileSize added to the querystring, and now all works well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top