I have an application that uses a DLL to make API calls. Is there a way to prevent my application from crashing when the DLL crashes? I tried putting a try/catch around each call, but that doesn't work.
If the DLL is a third-party library you cannot modify you can load and execute the code in a separate AppDomain.
If it is your own code it would be best to fix the bugs first. A NullReferenceException almost always indicates a bug in your code that should be fixed.
As you are not able to catch the exceptions with a try/catch blog I would assume that the exception occurs on another thread. Either wrap the method(s) on the other thread within a try/catch block or use the AppDomain.UnhandledException event to catch it.
It is not the DLL that crashes, it is your thread that suffers the heart attack. It cannot continue, which usually means your process is done for as well. Trying to handle the exception is rarely possible, you don't know what kind of damage was done.
Running it in a separate process is the only reasonable workaround. Although trying to recover from this process suddenly dying is unreasonably hard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With