1

I need to reference a dll to instantiate an object. There are cases where the dll will not exist because the Phidget drivers are not installed on those systems. In those cases I just want to catch the DllNotFoundException, but it's not working. I even tried nesting catch statements between the type reference and the instantiation, but still not catching. Any help would be appreciated.

enter image description here

Karmacon
  • 3,128
  • 1
  • 18
  • 20
  • Have you tried `catch (System.DllNotFoundException)`? I know it shouldn't make a difference if you have a `using System;` statement but all the google results I've found specifically use the full namespace in the `catch` statement. – Lews Therin Jun 14 '18 at 19:25
  • Also have you tried just `catch (Exception ex)` to see if it catches it then? – Lews Therin Jun 14 '18 at 19:27
  • @LewsTherin Yes I tried both suggestions but same result. – Karmacon Jun 14 '18 at 19:30
  • Huh, seems to work here: https://dotnetfiddle.net/kN0V5J. I would try restarting Visual Studio. – Lews Therin Jun 14 '18 at 19:31
  • Another thing you can try is to clean solution and then rebuild solution. – Lews Therin Jun 14 '18 at 19:33
  • @LewsTherin I think the issue is that the exception is being thrown within the dll and it's not bubbling up. I believe that my referenced dll internally references another dll on the system. If that system dll is not installed, it throws the exception. Does that make sense? – Karmacon Jun 14 '18 at 19:36
  • Just found this, which is the same problem I'm having https://stackoverflow.com/questions/33785259/catch-dllnotfound-exception?rq=1 – Karmacon Jun 14 '18 at 19:39

1 Answers1

0

I think you are seeing a problem very similar to Why does short-circuiting not prevent MissingMethodException related to unreachable branch of logical AND (&&)?

Put the catch in a separate function, one that does not use typeof(Phidget22.DigitalInput) or any other identifier from the DLL.

Exceptions such as TypeLoadException, MissingMethodException, and DllNoFoundException can be called by the JIT compiler before execution of your function starts (and then catch blocks in the function will have no effect).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720