Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching a DLL Crash

Tags:

c#

c#-4.0

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.

like image 875
Robert Avatar asked Dec 21 '25 15:12

Robert


2 Answers

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.

like image 61
Dirk Vollmar Avatar answered Dec 24 '25 12:12

Dirk Vollmar


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.

like image 35
Hans Passant Avatar answered Dec 24 '25 12:12

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!