4

I've a 64bit system with Win7 and I use VS2010 with .Net Framework 4.0.

In a Winform application I get an Exception from a third-party component, I'm trying to catch this exception with a Try-Catch but it doesn't work!

Why?

enter image description here

UPDATE

If I un-check CLR Exceptions It works properly but I'm forced to check it due to this known problem: Why the form load can't catch exception?

If I press F10 it continues entering in catch statement!!

Community
  • 1
  • 1
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • That seems odd. `Exception` should of course catch this. Have you tried explicitly catching the exception type thrown? – MoonKnight May 25 '12 at 09:40
  • I tried to fix the grammar in the title and then I realized it was deliberate. Lolled. (SO rejects a new question with the same title as an old one). – dandan78 May 25 '12 at 09:42
  • Does your program execute after the catch or is it crushing? – Grzegorz W May 25 '12 at 09:42
  • 2
    You've configured the debugger to break when the exception is thrown. Have you tried to press F5 when the program stops inside the `try`block? – Stephan May 25 '12 at 09:43
  • If you mean that you see that in your VisualStudio, it's perfectly fine and it **has** to work in that way, because you choosed to handle them. – Tigran May 25 '12 at 09:43
  • 1
    What's the full stack-trace? CAENRFIDException, does extend System.Exception, I assume. Have you tried checking the other Exception check-boxes, in case somehow it's one of those? Also, 'Exception not caught'. The paradigm of catch is catch/caught/caught, it's a strong-verb. :) – nicodemus13 May 25 '12 at 09:44
  • `Exception` I suppose generally catches managed exceptions, try using catch { //Log any custom message } and that should catch all exceptions..probably this answer might help you http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code – S2S2 May 25 '12 at 09:52
  • Did you ever try pressing `F5` like Stephan suggested? – Cody Gray - on strike May 25 '12 at 10:05
  • Just to state the obvious. If the component you are calling is handling the exception it isn't going to bubble up to your exception handler... – Yaur May 25 '12 at 10:12
  • 1
    Okay, if pressing `F11` or `F5` or some other "continue" button in the debugger causes the `catch` block to be executed, then that's expected behavior. What are you hoping would happen, instead? You have the debugger configured to break when CLR exceptions are thrown in order to work around the 64-bit environment problem. That means the debugger is going to break each time one is thrown. If you *continue*, your `catch` block will execute per usual. – Cody Gray - on strike May 25 '12 at 10:13
  • Yes Cody! Checking CLR Exceptions drives me into this unexpected behavor! Now I'got understand it, Thx to all! – danyolgiax May 25 '12 at 10:16

2 Answers2

1

Seems that you are using calling an unmanaged dll here. Try catching this exception using ExternalException Class. This may work for you but once you've gone outside of the .NET runtime's control, it's entirely up to the unmanaged code; there's nothing the .NET runtime can do.

ABH
  • 3,391
  • 23
  • 26
  • 2
    Even if it's an unmanaged dll... `ExternalException` has `Exception` as a base class. So it is caught by `catch (Exception)` – Stephan May 25 '12 at 09:53
0

Have you got the following setting unchecked:

Break when exceptions cross AppDomain or managed/native boundaries (Managed only)

in your Tools->Options->Debugging->General tab for Visual Studio 2010

Its explained in MSDN here

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
S2S2
  • 8,322
  • 5
  • 37
  • 65