Click here to Skip to main content
15,886,842 members
Please Sign up or sign in to vote.
3.40/5 (3 votes)
See more:
I'm trying to code an Application_Error() event handler for my application.

Code works, but if I removed file permissions on the file that the program is trying to access the error that called the Application Error() handler is displayed as a server error instead of an IO Exception.

This code is loosely based off of this: http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/aspnet-error-handling[^]

C#
protected void Application_Error(object sender, EventArgs e)
        {
                Exception exc = Server.GetLastError();
                Guid g = Guid.NewGuid();
                if (exc is HttpUnhandledException)
                {
                    if (exc.InnerException != null)
                    {
                        #region WriteToLog
                        //write and modify permissions required for local\IIS_IUSERS on App_Data
                        string filepath = "~/App_Data/ErrorLog.txt";
                        filepath = HttpContext.Current.Server.MapPath(filepath);

                        if (!File.Exists(filepath))
                            File.Create(filepath);

                        StreamWriter sW = new StreamWriter(filepath, true, Encoding.UTF8);
                        try
                        {
                            sW.WriteLine("********** {0} **********", DateTime.Now);
                            sW.Write("Guid: ");
                            sW.WriteLine(g.ToString());
                            if (exc.InnerException != null)
                            {
                                sW.Write("Inner Exception Type: ");
                                sW.WriteLine(exc.InnerException.GetType().ToString());
                                sW.Write("Inner Exception: ");
                                sW.WriteLine(exc.InnerException.Message);
                                sW.Write("Inner Source: ");
                                sW.WriteLine(exc.InnerException.Source);
                                if (exc.InnerException.StackTrace != null)
                                {
                                    sW.WriteLine("Inner Stack Trace: ");
                                    sW.WriteLine(exc.InnerException.StackTrace);
                                }
                            }
                            sW.Write("Exception Type: ");
                            sW.WriteLine(exc.GetType().ToString());
                            sW.WriteLine("Exception: " + exc.Message);
                            //sW.WriteLine("Source: " + source);
                            sW.WriteLine("Stack Trace: ");
                            if (exc.StackTrace != null)
                            {
                                sW.WriteLine(exc.StackTrace);
                                sW.WriteLine();
                            }
                        }
                        finally
                        {
                            sW.Close();
                        }

                        #endregion

                        string message = (exc.InnerException.Message != null) ? exc.InnerException.Message : exc.Message;
                        string pageToTransferTo = "~/ErrorPage.aspx?GUID=" + g.ToString() + "&Message=" + message;

                        Server.Transfer(pageToTransferTo);

                    }
                }
        }
Posted
Updated 15-Aug-13 7:47am
v5

1 solution

The first mistake I can see is this:
C#
try {
   //...
} catch (Exception ex) {
   throw ex;
}
is functionally strictly equivalent to not handling exceptions at all and
C#
try {
   Something();
} catch (Exception ex) {
   throw ex;
} finally {
   SomethingElse();
}
is functionally strictly equivalent to
C#
try {
   Something();
} finally {
   SomethingElse();
}

Beyond functional equivalence: you are just wasting time and resources for doing nothing. Just don't catch and handle this way. In first case, don't use try-catch at all.

This and your other question show that you have no clue on two critically important topics: exceptions and events. I don't know a good way to help you more effectively before you do the following: pause all you are doing and study this two topic in detail. Practice on very simple examples, not on HTTP, networking UI or any other advanced topic. Seriously. Before you master it, you would just waste your time.

[EDIT]

After getting this answer, OP fixed the problems mentioned above, in the body of the question. Of course, it still requires to think at the major problem.

—SA
 
Share this answer
 
v2
Comments
Kyle Gottfried 15-Aug-13 13:01pm    
You can be critical all you want, and I agree, I have not had as much experience as I should have on event handlers.
This code sample is sloppy, I know that. Primarily it's sloppy because I was trying to get information back besides a Server error for the error that called the Application_Error() event.

The was a difficult issue because something that was working when debugging in an Local IIS Web Server there were no errors and when I ran it on IIS without the aid of VS it failed.

The least you could to is try to help someone out with a question not just slam them for some sloppy code and walk out the door. It's rude and leaves everyone in a bad mood.

None of my other code makes the same simply mistakes that I made here.

And my previous question? It was a perfectly good question. As an event is raised when the application has an error and is handled by the Application_Error() method, an error inside that method may simply raise the Application Error event again causing an endless loop. I have no idea why anyone closed the question.

And for your information, I figured out what the problem was myself. IIS did not have Create or Modify permissions on the folder which the Error log file was stored.
Sergey Alexandrovich Kryukov 15-Aug-13 13:05pm    
I would gladly answer your other question if not your rudeness with someone who politely talked to you and spent good time trying to help you.
Sorry, no more.

Note: OP removed last line from the comment, to look it nos so outrageous. Much better.

—SA
Kyle Gottfried 15-Aug-13 13:42pm    
That was me that removed the last line, not an op.
Sergey Alexandrovich Kryukov 15-Aug-13 13:45pm    
I know. Unfortunately, we have bad jargon here: OP is you, it means Original Poster or something like that. Sorry about confusion.
—SA
Sergey Alexandrovich Kryukov 15-Aug-13 13:44pm    
Okay, I gave you recommendation and strongly suggest you follow my advice. Even if you solved your problem, it will haunt you later until you learn those critically important topics.
Your attitude of confrontation when you hear something you don't like won't server you well. If I did not advise you to learn it all, would you feel better? You actually did not solve the root of the problem, correct handling of application error, you just removed the error, but this is not the only possible one.
—SA

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



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