0

We have a desktop application and are using a third party that wrote by C++. We are using the library's methods in our whole application.

We have handled AppDomain.CurrentDomain.UnhandledException and Application.ThreadException events for catching the unhandled exceptions, and we are expecting to catch all the exceptions in the UnhandledException event. Now the problem is when some methods in the library throw an exception, UnhandledException and ThreadException events can not catch the exception, and the entire application crashes.

Why don't we use "try/catch"? Because there are a lot of classes that use the library, and we are looking for a general solution to the problem.

Question: Why can we not catch the exception in UnhandledException event which raises from the method uses the library's methods?

Request: Is there any way to catch the exceptions on a general scale?

Thank you so much for your attention and participation.

  • Not sure but if the C++ lib is not written with NET in mind you could be out of luck. See https://stackoverflow.com/questions/4223470/c-sharp-not-catching-unhandled-exceptions-from-unmanaged-c-dll – Steve Sep 14 '20 at 09:42
  • If it is a windows error you may be able to use following : https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error?view=netcore-3.1 – jdweng Sep 14 '20 at 09:48
  • Does this answer your question? [How can I handle the exception which throws on an external dll?](https://stackoverflow.com/questions/14976959/how-can-i-handle-the-exception-which-throws-on-an-external-dll) –  Sep 14 '20 at 09:50
  • What you say cannot be true. `AppDomain.CurrentDomain.UnhandledException` will ALWAYS catch all unhandled exceptions (unless you subscribe on it too late), and `Application.ThreadException` should be invoked for all threads (not just the UI thread). – jgauffin Sep 14 '20 at 12:37

0 Answers0