Final managed exception handler in a mixed native/managed executable?

Question :

Final managed exception handler in a mixed native/managed executable?,

Answer :

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 CWinApp::ProcessWndProcException works.

The two events suggested in Jeff’sApplication.ThreadException and AppDomain.CurrentDomain.UnhandledException, are not raised.

Can anyone suggest a way to provide a final managed exception handler for a mixed executable?


Update:

It appears that these exception handlers are only triggered downstream of Application.Run 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 System.Exception and if you want a callstack you’re going to have to roll your own walker.

Read More  How can I convert IEnumerable to List in C#? June 10, 2022 by Eiji I am using LINQ to query a generic dictionary and then use the result as the datasource for my ListView (WebForms). Simplified code: Dictionary dict = GetAllRecords(); myListView.DataSource = dict.Values.Where(rec => rec.Name == "foo"); myListView.DataBind(); I thought that would work but in fact it throws a System.InvalidOperationException : ListView with id ‘myListView’ must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true. In order to get it working I have had to resort to the following: Dictionary dict = GetAllRecords(); List searchResults = new List(); var matches = dict.Values.Where(rec => rec.Name == "foo"); foreach (Record rec in matches) searchResults.Add(rec); myListView.DataSource = searchResults; myListView.DataBind(); Is there a small gotcha in the first example to make it work? (Wasn’t sure what to use as the question title for this one, feel free to edit to something more appropriate) Try this: var matches = dict.Values.Where(rec => rec.Name == "foo").ToList(); Be aware that that will essentially be creating a new list from the original Values collection, and so any changes to your dictionary won’t automatically be reflected in your bound control. Categories question Post navigation .NET unit testing packages Authenticating Domain Users with System.DirectoryServices

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 try ... catch (Exception^). For instance, CWinApp::Run. 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.

,

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

The CLR relies on the SEH unhandled exception filter mechanism to catch unhandled exceptions.

That’s the answer Final managed exception handler in a mixed native/managed executable?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Is this the proper use of a mutex?

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you