views:

445

answers:

3

If you throw an exception from inside an MFC dialog, the app hangs, even if you have a catch block in your code. It refuses to respond to the mouse or keyboard, and the only way to shut it down is to use Task Manager.

Why I'm posting this question

To my shame, there is a popular shrink-wrapped application that hangs every time it encounters an exceptional error in a modal dialog. When we made a massive shift from integer error codes to exceptions, I was responsible for choosing std::exception as the base class for the thrown exceptions. It wasn't until a huge amount of work went into the conversion that our testing uncovered this problem, and by then it was too late to change. Hopefully this question/answer will keep someone from making the same mistake.

+6  A: 

The code for CDialog::DoModal makes the dialog modal by disabling the parent window. When the dialog code returns, the window is reenabled. There is an explicit catch for CException* errors, but not for any other kind of thrown exception; thus the parent window never gets reenabled.

Change your code to throw a pointer to any exception derived from CException, and you'll fix the problem.

Mark Ransom
+3  A: 

If you are interested in learning about how Windows detects apphangs we have added some posts to this on the Windows Error Reporting blog:

Let there be hangs part 1 of 4

Let there be hangs part 2 of 4

Let there be hangs part 3 of 4

Let there be hangs part 4 of 4

Important to note is that this information when sent through Microsoft's Windows Error Reporting gets communicated to the software developers to try and fix these issues. If you are sending in your error reports you WILL help fix issues that are occuring on your PC!

I am a Program Manager at Microsoft on the Windows Error Reporting team.

Thank you - those links are very interesting.
Mark Ransom