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
2010-04-14 06:54:10
+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
2010-04-14 06:55:55
+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
2010-04-14 07:13:57
I want to display my message.
Ram
2010-04-14 07:35:44
Then you need to catch exceptions.
Windows programmer
2010-04-14 08:53:58
Generic exception??? or any specific one?
Ram
2010-04-14 12:49:28
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
2010-04-15 00:12:36