Global exception handler not working as expected

Hi All,
I am trying to write a code that generates the crash dump for the application in .Net. I have tried using the Application.ThreadException, Application.SetUnhandledExceptionMode and AppDomain.CurrentDomain.UnhandledException to catch the exception. I was able to catch the Divide by Zero and Null Reference Exception in the Application_ThreadException. But I am unable to catch the stackoverflow exception. I need your help to catch all errors & Exceptions so that I can take a crash dump and look at them offline for investigations.
Currently we are working on Windows XP 32 bit machine. I use VS2008 and .Net 3.5.

static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());           
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {            
            MessageBox.Show("Reached Application_ThreadException");
            Application.Exit();
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show("Reached Application_ThreadException");
            Application.Exit();
        }

Open in new window


Thanks in Advance
Murthy
LVL 1
Anjeneya MurthySr. ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
The StackOverflowException is one of those rare exceptions that you cannot catch because it terminates the application.

Read the following, that comes straight from the documentation for the class:

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.

Exceptions that cannot be caught by a try-catch block cannot be caught by a Global Exception handler either.
0
Anjeneya MurthySr. ConsultantAuthor Commented:
Thanks for the respone.
If this is not possible to catch directly by introducing the code in the same application, is it possible to catch it from a separate application.
I saw some articles about exceptions that they are specific to threads and specific to process. if that is the case, we will not be able to catch them from other process either!
I wonder, how the tools like debug diag and Adplus catches our exceptions and generates the dumps!!
0
Jacques Bourgeois (James Burger)PresidentCommented:
I do not remember seeing software that can do with your applications things that you cannot do in the code the application itself, so I would be very surprised if some utility could get that exception.

You might want to try the Windows API Code Pack available for free on the Microsoft site. It contains a few classes created to help with application recovery and restart when there is a crash. It may contain something of interest to you.
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Anjeneya MurthySr. ConsultantAuthor Commented:
I Tried the sample with stack overflow exception. I installed the DebugDiag tool and set the rule to generate the dump on crash. Surprisingly, the application did not crash nor the dump was generated!
Then I set the DebugDiag to create a dump on first chance exception and selected stack overflow exception. This time when I ran the application, the application crashed and the dump was also generated!
If DebugDiag can catch the stackoverflow exception, why cant my code? :(
Please help.
0
Anjeneya MurthySr. ConsultantAuthor Commented:
Also, from the link http://msdn.microsoft.com/en-us/library/system.stackoverflowexception(v=VS.90).aspx, what is the purpose of System.StackOverflowException in .Net 2.0 and higher versions when we cannot catch it!
0
Jacques Bourgeois (James Burger)PresidentCommented:
A stack overflow is something that should not happen. It is a programming error that comes from bad design in the application, not from an external event such as a FileNotFounException or SQLException that depends on external stuff over which the programmer has not direct control.

The stack plays a very important part in the control of program execution by knowing for instance where to come back from a call to method and other stuff of that order. When the stack is full, the application has no way to go on, so it cannot even return to you error handler.

This exception is almost always caused by recursion: you have a method that calls itself indefinititely. This should not happen if the program is designed properly. When you get one, note what the application was doing. Find recursions in that portion of the code and make sure to have a way, such as a counter, to stop the recursion when it goes out of control. It will never happen again if your design is good.
0
Anjeneya MurthySr. ConsultantAuthor Commented:
Hi JamesBurger,
Thanks for the response. I agree that writing program that causes stack overflow is a bad design. But the application being developed is not done by a single person. More than 50 people are wiring code for this application! Just for safety, I wanted to introduce this feature of catching all unhanlded exceptions and generate the crash dump.
The Idea originated because there are tools that catches exceptions like stackoverflow (See DebugDiag that catches this exception if it was set to catch stackoverflow firstchance exception!). I wanted to use this technique in my application. We do not want everyone who uses our application to use third party tools to genarate dumps for two reasons:
1. It is a burden to the end users asking them to install the third party tool if they see any issues in our applications.
2. The time is spent without any good progress. (customer informing us of the issue, we asking to install the third party application, then he tries to reproduce the issue and then once the crash dump is generated, then the actual work begins! )

The question still remains for me!
How could the tools like Debug Diag catch these exceptions, that we cant!! :(
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
DebugDiag works with COM. The memory model and memory management in COM is quite different from what it is in .NET.

We used to be able to catch StackOverflow errors in VB6, that was COM based. Not in .NET, and I am quite sure that it has to do with the way .NET handles memory to prevent memory leaks and add security.

Good luck.
0
Anjeneya MurthySr. ConsultantAuthor Commented:
Closing this as I did not get the right working solution. I knew this is not easy, But was and am still curious as to how the DebugDiag works even for Stack overflow first chance exception.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Look at answer 37362946, taken straigth from the documentation.

If the author of the question is not satisfied by the answer, he should complain to Microsoft. On our side, we did our job. Answering that something is not possible, when it is not, is the right answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.