开发者

Do we really need to catch exception for P/Invoke methods?

I am using P/Invoke methods in my .NET application. As the functions are C++ functions; each one has return type lik开发者_如何学Pythone 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?


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


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 :)


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜