views:

52

answers:

3

I am using P/Invoke methods in my .NET application. As the functions are C++ functions; each one has return type like int, intptr or any struct. The return type is enough to tell me if the function was successful or not. Do I still need to catch generic or COM exception in this case?

+2  A: 

PInvoke call still may fail if Dll is not found, or doesn't contain required function.

Alex Farber
+2  A: 

In my opinion you should always use try/catch when invoking an external component. You never know what can be returned. Prepare for the unexpected :)

Rhapsody
+1  A: 

C++ functions can generate exceptions.

Windows APIs can generate exceptions whether they're called directly from P/Invoke or from C++ functions.

Is it OK for your users to see exception messages (error messages) from Windows or do you want your program to display your message to them?

Windows programmer
I want to display my message.
Ram
Then you need to catch exceptions.
Windows programmer
Generic exception??? or any specific one?
Ram
If you know some specific exceptions that you think you can recover from then of course you want to catch them specifically. Whether or not you do that, you still have to catch all generic random exceptions (because you want to display your message instead of letting the user see the ordinary Windows message).
Windows programmer