<p>I have an MFC application compiled with /clr and I'm trying to implement a final handler for otherwise un-caught managed exceptions. For native exceptions, overriding <code>CWinApp::ProcessWndProcException</code> works.</p>

<p>The two events suggested in Jeff's <a href="http://www.codeproject.com/KB/exception/ExceptionHandling.aspx" rel="nofollow noreferrer">CodeProject article</a>,<code>Application.ThreadException</code> and <code>AppDomain.CurrentDomain.UnhandledException</code>, are not raised.</p>

<p>Can anyone suggest a way to provide a final managed exception handler for a mixed executable?</p>

<hr>

<p>Update:</p>

<p>It appears that these exception handlers are only triggered downstream of <code>Application.Run</code> or similar (there's a worker thread flavor, can't remember the name.) If you want to truly globally catch a managed exception you do need to install an SEH filter. You're not going to get a <code>System.Exception</code> and if you want a callstack you're going to have to roll your own walker.</p>

<p>In an MSDN forum question on this topic it was suggested to override a sufficiently low-level point of the main MFC thread in a <code>try ... catch (Exception^)</code>.  For instance, <code>CWinApp::Run</code>.  This may be a good solution but I haven't looked at any perf or stability implications.  You'll get a chance to log with a call stack before you bail and you can avoid the default windows unahndled exception behavior.</p>

[{'answer_id': 9432, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': "<p>Using those two exception handlers should work. Are you sure you've added them in a place where they're going to be called and properly set (ie, in your application's <em>managed</em> entry point -- you did put one in, right?)</p>\n"}, {'answer_id': 15146, 'author': 'Aidan Ryan', 'author_id': 1042, 'author_profile': 'https://Stackoverflow.com/users/1042', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Using those two exception handlers should work.</p>\n</blockquote>\n\n<p>Why "should?"</p>\n\n<p>The events are not raised using the below:</p>\n\n<pre><code>extern "C" void wWinMainCRTStartup();\n\n// managed entry point\n[System::STAThread]\nint managedEntry( void )\n{\n    FinalExceptionHandler^ handler = gcnew FinalExceptionHandler();\n\n    Application::ThreadException += gcnew System::Threading::ThreadExceptionEventHandler(\n                                        handler,\n                                        &amp;FinalExceptionHandler::OnThreadException);\n\n    AppDomain::CurrentDomain-&gt;UnhandledException += gcnew UnhandledExceptionEventHandler(\n                                                        handler,\n                                                        &amp;FinalExceptionHandler::OnAppDomainException);\n\n    wWinMainCRTStartup();\n\n    return 0;\n}\n\n// final thread exception handler implementation\nvoid FinalExceptionHandler::OnThreadException( Object^ /* sender */, System::Threading::ThreadExceptionEventArgs^ t )\n{\n    LogWrapper::log-&gt;Error( "Unhandled managed thread exception.", t-&gt;Exception );\n}\n\n// final appdomain exception handler implementation\nvoid FinalExceptionHandler::OnAppDomainException(System::Object ^, UnhandledExceptionEventArgs ^args)\n{\n    LogWrapper::log-&gt;Error( "Unhandled managed appdomain exception.", (Exception^)(args-&gt;ExceptionObject) );\n}\n\nBOOL CMyApp::InitInstance()\n{\n    throw gcnew Exception("test unhandled");\n    return TRUE;\n}\n</code></pre>\n'}, {'answer_id': 192733, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 3, 'selected': True, 'text': '<p>Taking a look around the internets, you\'ll find that you need to install a filter to get the unmanaged exceptions passing the filters on their way to your AppDomain. From <a href="http://blogs.microsoft.co.il/blogs/sasha/archive/2007/12/30/clr-and-unhandled-exception-filters.aspx" rel="nofollow noreferrer">CLR and Unhandled Exception Filters</a>:</p>\n\n<blockquote>\n  <p>The CLR relies on the SEH unhandled exception filter mechanism to catch unhandled exceptions.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/8704', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1042/']