2

I have a method that calls an unmanaged library from a background thread. I put a try catch block around the unmanaged call but it is never caught. The just in time debugger catches it instead. What am I doing wrong?

 public bool TurnOn() { var connectionSucceeded = false; try { connectionSucceeded = turnOn(); //Unmanaged call } catch { connectionSucceeded = false; //Never reaches this block } return connectionSucceeded; } 
2
  • you use .net 4.0? In .net 4.0 this problem may be, when in 3.5 not. Commented Mar 19, 2011 at 19:09
  • 1
    Document what you see in the Output window. Commented Mar 19, 2011 at 19:43

2 Answers 2

1

Is the debugger setup to handle unmanaged exceptions when they are thrown?

Check the setting in Debug>Exceptions

Also, is the unmanaged exception definitely thrown on the thread directly being called - and not in some async operation on a different thread?

Sign up to request clarification or add additional context in comments.

6 Comments

Either way it should either show up in the catch block or the UnhandledException event. It does not do either.
If it's thrown on a new thread - one created by the native code - then I don't think it will show up in your handler.
How would I catch something like that?
AFAIK, I'm not sure you can - if it's a native exception on a thread without any native try/catch handling, then I think that's game over. But first of all, is there any way you can check that this is actually the problem - e.g. what does the native stack trace show you? What is the native "turnon"? Is it something you have the pdb/dbg symbols or the source code for?
I do not have any pdb/dbg symbols or any access to the source.
|
1

I fully agree with Stuart answer.. In this case you could try catch exception on application level:

AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionEventRaised; 

Plz see also this answer: AppDomain.CurrentDomain.UnhandledException not firing without debugging

3 Comments

Sorry let me be more clear. That event does fire, just not in the case of unmanaged exceptions.
Plz look to referenced answer and try it.
The referenced answer uses winforms methods as part of the solution, I'm using wpf

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.