码迷,mamicode.com
首页 > 其他好文 > 详细

How to handle exception in managed code and unmanaged code

时间:2015-01-08 22:47:50      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:exception   c++   

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

标签:exception   c++   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!