Do we really need to throw exceptions for P / Invoke methods?

I am using P / Invoke methods in my .NET application. Since functions are C ++ functions; they each have a return type such as int, intptr, or any structure. The return type is enough to tell me if the function was successful or not. Should I still be catching the generic or COM exception in this case?

+2


a source to share


3 answers


C ++ functions can throw exceptions.

The Windows API can throw exceptions, whether they are thrown directly from P / Invoke or from C ++ functions.



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

+1


a source


The PInvoke call may still fail if the Dll is not found or does not contain the required function.



+2


a source


In my opinion, you should always use try / catch when calling an external component. You never know what to return. Prepare for the unexpected :)

+2


a source







All Articles