首页 > 其他 > 详细

How to handle exception in managed code and unmanaged code

时间:2015-01-08 22:46:45      阅读:404      评论:0      收藏:0      [点我收藏+]

As we known, try...catch mechanism is a quite common feature for the high level languages like java or C#. Although C++ proclaimed  that it supports this mechanism, the memory management limitation of C++ makes this try...catch function is weak.

 

C++ cannot automatically captures the exception like C# or Java, so in case of exception happens, we must throw explicitly.

 

// exceptions#include <iostream>usingnamespace std;int main () {  
Char* pEx = 0;
...
try  { 
if(pEx == 0)   throw 20;  }  catch (int e)  {    cout << "An exception occurred. Exception Nr. " << e << ‘\n‘;  }  
return 0;
}

But if we are coding with managed and unmanaged code, how to handle the exception?

Actually for the managed part try block no doubt can capture the exception, for the un managed part we can use one special way.

Try catching using the ExternalException class

The base exception type for all COM interop exceptions and structured exception handling (SEH) exceptions.

 

[SerializableAttribute][ComVisibleAttribute(true)]
public ref class ExternalException : public SystemException

 

As the sample above, makes your target clause derived from ExternalException class, then internally the function can capture the exception.

 


How to handle exception in managed code and unmanaged code

原文:http://blog.csdn.net/yu444/article/details/42532001

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!