3

I'm a beginner with the windows api so there must be something i don't understand here. In my main function i'm using a try-catch to catch all uncaught exceptions, but for some reason, exceptions i throw from somewhere else in the code are never caught. My application uses a single (main) thread.

I'm throwing like this :

throw "ClassName::methodName() - Error message";

And catching the exceptions outside of the message loop :

try {
    while(GetMessage(args...)) {
        TranslateMessage(args...);
        DispatchMessage(args...);
    }
}
catch( const char * sExc ) {
    ::MessageBox(args...);
}

I first thought it was a problem of types mismatching, but then i added a catch(...) with ellipsis and i still caught nothing. If you ask, yes, i'm sure the exception is thrown. Isn't it a problem related to some kind of asynchronousness or something like that ?

Thanks for your help !

Paul R
  • 208,748
  • 37
  • 389
  • 560
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • Where is the string literal being thrown? Is it on another thread? – doctorlove Sep 13 '13 at 13:03
  • I tried removing the const, but same result. I would have been surprised if it was that though, i've been doing like this in other codes without problem about const, and the thrown string is a constant. @Joachim Pileborg : No i'm not catching it somewhere else. The debugger fails with the following error message : "Unhandled exception at 0x7c812fd3 in xxxxxxx.exe: Microsoft C++ exception: char at memory location 0x0012d124.." – Virus721 Sep 13 '13 at 13:04
  • @doctorlove As i said, this is not a multithreaded application, so it comes from the same thread. I assume that DipatchEvent() is calling my window procedure, which is where the exception comes from. – Virus721 Sep 13 '13 at 13:09
  • You're mentioning the debugger. The default settings in Visual Studio debugger is to break when an exception is thrown, before it actually begins flight (i.e. before stack unwinding and propagation starts). Try hitting `F5` when the program breaks - the debugger is likely to ask "pass exception to program being debugged?" – Angew is no longer proud of SO Sep 13 '13 at 13:12
  • @Virus721, put a breakpoint on the line which says "throw". When the breakpoint is hit, use F11 to step over that line. Control should transfer to whatever has eaten the exception. – Ben Sep 13 '13 at 13:26
  • Well i tried compiling and running a release version, but it still didn't work : i got an error message about a runtime error, contact your application's support bla bla bla, and then the usual Windows XP error report pup-up. – Virus721 Sep 13 '13 at 13:29

2 Answers2

4

It depends on the specific message that's getting dispatched. But no, not all of them allow the stack to be unwound through Windows internal code. Particularly the messages that involve the window manager, WM_CREATE for example. There's a backstop inside Windows that prevents the stack from being unwound past that critical code. There's also an issue with exceptions in 32-bit code that run on the 64-bit version of Windows 7, they can get swallowed when the message needs to traverse the Wow64 boundary several times. Fixed in Windows 8.

On later Windows versions this can also activate "self-healing" code, automatically activating an appcompat shim that swallows the exception. You'll get a notification for that, easy to dismiss. You'll then see the first-chance exception notification in the VS Output window but your program keeps running. Okayish for the user perhaps but not great while you are debugging of course. Run Regedit.exe and navigate to HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Persisted and check if your program is listed there. Just delete the entry.

Long story short, it is not safe to catch exceptions outside of the message loop. You have to do it inside the window procedure.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for your answer. I already moved the try-catch to the window procedure, but i'm getting the same results. The throw and catch types match, and the exception IS thrown, so i don't get what's not working. – Virus721 Sep 14 '13 at 10:58
  • I've got nothing to look at. I already mentioned the registry key. Debug + Exceptions, ensure that the Thrown checkboxes are turned off. – Hans Passant Sep 14 '13 at 11:13
1

You are talking about "Windows Structured Exception Handling" (http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx). C++ exceptions are not thrown.

If you want to go the troublesome route: _set_se_translator

See also: Can a C program handle C++ exceptions? (The windows API is not C++)

Community
  • 1
  • 1