0

I know that few similar topics have been created on StackOverflow (e.g there and there). I have a well-known problem - user unhandled Exceptions thrown in Visual Studio in Windows 7 64bit are not handled by IDE debugger, so debugger does not break in suitable code line. Because I don't want to catch all exceptions by enabling "Throwing" checkBoxes in Tools->Exceptions... menu, I have tried to use Microsoft article solution.

Applying MS solution caused that, the situation has changed, but VS debugger still does not run correctly.

Currently, when new Exception is thrown, I see system internal error message and then VS debugger will correctly stops on error line BUT only for less than one second and application exits...

Do you have some others solution to resolve this bug? Programming with so called silent exceptions are very uncomfortable...

EDIT: I hope, that now my question is less ranty...

Community
  • 1
  • 1
Viper
  • 597
  • 1
  • 8
  • 24
  • This is a very ranty question. I can't think of a Windows quirk that's been better characterized as this one, including a **large** number of possible workarounds. Including Win8 where it has been solved. Having no appreciation for the *very* difficult problem they had to solve is fine, whining about it and asking for yet another workaround makes this very unconstructive. – Hans Passant Jan 01 '13 at 15:39
  • @HansPassant I've asked about other solutions because presented solutions aren't satisfied for me. Fortunately, just a moment ago I have found great solution. Instead of registry modifications etc., the great fix is to add this line to Program.cs System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException). It works for me - I hope that it will solve my problem in all ways. What is interesting, that this line modify VS behavior only, so it seems that VS way of working is also connected with this bug. – Viper Jan 01 '13 at 16:06
  • @Viper: If you've found a solution that works, then you should [post it as an answer](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). Also, you _really_ should edit your question to be less ranty, otherwise it's likely to be closed (it takes 5 users to vote to close it, and there are already 3 votes so far). – Ilmari Karonen Jan 01 '13 at 23:13
  • @IlmariKaronen I have edited my question according to your advices. I also post "my" solution. Unfortunately the code syntax is not highlighted. I hope that I have done everything correct. – Viper Jan 02 '13 at 12:08
  • Thanks! I fixed your syntax highlighting -- not sure why it didn't just work, but adding an explicit language hint solved it. Once the software lets you, you can also mark your own answer as accepted by clicking the check mark icon next to it. – Ilmari Karonen Jan 02 '13 at 17:43
  • I really do not understand why this topic has been closed... The described problem is real and my solution is also correct and working. – Viper Jan 04 '13 at 12:21

1 Answers1

1

In my case none of registry modifications were needed. According to this topic and Redd answer, I modified my Program.cs file in such manner:

static void Main()
{
    try
    {
        System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);
        AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

        var form = new MainForm();
        form.ShowDialog();
    }
    catch (Exception e)
    {
        HandleUnhandledException(e);
    }
    finally
    {
        // Do stuff
    }
}

private static void HandleUnhandledException(Object o)
{
    // TODO: Log it!
    Exception e = o as Exception;

    if (e != null)
    {

    }
}

private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
{ 
    HandleUnhandledException(e.ExceptionObject);
}

private static void OnGuiUnhandedException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    HandleUnhandledException(e.Exception);
}

Now my debugger works OK, so it stops on unhandled exceptions and doesn't stop on handled exceptions. It looks like Microsoft solves silent exceptions problem in SP1 for Windows 7 64 bit at OS level, but enforcing correct working of VS still needs some user/programmer actions.

Community
  • 1
  • 1
Viper
  • 597
  • 1
  • 8
  • 24