Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more: , +
hai..

After hosting the application in server Application_Error event is not get fired.

The Event is as follows..

C#
void Application_Error(object sender, EventArgs e) 
    {
        try
        {

        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "<!---" + "Error Caught in Application_Error event <br> " + "Error     in:  <br> Error Message:" + objErr.Message.ToString() +
         " <br> Stack Trace:" + objErr.StackTrace.ToString()
         + "-->";
            Response.TrySkipIisCustomErrors = true;
            Session["Erroronly"] = err.ToString();

            Response.Redirect("~/Login.aspx?TimeOut=Yes");
        }
        catch
        {

        }
}


Locally it runs perfectly.. but in server the error msg is shown somthing as object reference not set to an instance of an object etc..

any help would be appreciated..

Thank you.
Posted
Comments
shijuse 31-Jul-12 9:31am    
Please try this
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();




if (ex.Message == "File does not exist.")
{
throw new Exception(string.Format("{0} {1}", ex.Message, HttpContext.Current.Request.Url.ToString()), ex);
}
}
http://stackoverflow.com/questions/6508415/application-error-not-firing-when-customerrors-on

Sorry that is does not help you immediately, but it can help you to approach the problem from the right side. First of all, there is no such event as Application_Error. This is supposed to be an event handler, a completely different thing. It is supposed to be added to the invocation list of an event using the "+=" operator, but how can we know if that operator was called or not?

So, use the debugger. Set a breakpoint on a very beginning of your event handler and another one on the line with "+=" operator. Write some application code to guarantee an exception. Make one step at a time.

Also, you are doing a big mistake by blocking propagation of exceptions by writing your try-catch block with empty catch block. This defeats the purpose of exception handling. Better remove your try-catch block at all. The purpose of structured exception handling mechanism is designed to avoid handling exceptions here and there. Normally, you need to catch all exceptions only on the top stack frame of the stack of each thread; and you might need to catch exceptions in few special cases, but blocking of the propagation is used very rarely (in particular, to work with some bad libraries which you cannot patch because source code is unavailable or if you cannot afford it).

If your search appears to be too complex, simplify the code to address only one isolated problem. In fact, instead of using your working code, create a minimal code sample to reproduce just one problem.

Please review these articles and see if you are missing something:
http://www.codeguru.com/csharp/.net/net_debugging/article.php/c19411/Web-Application-Error-Handling-in-ASPNET.htm[^],
http://blogs.x2line.com/al/articles/2646.aspx[^].

This is all I can advise based on your limited information.

Good luck,
—SA
 
Share this answer
 
The problem is accessing the session.
Notice that this works just fine for HTTP 500 (Server Error) errors, but if it's a HTTP 404 (File Not Found) error, the Session is always null.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900