How to handle exception in managed code and unmanaged code
2015-12-12 23:55
645 查看
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[code] std;int
main() {
Char* pEx = 0;
...
try[code] {
if(pEx == 0)throw
20; }catch
(int
e) { cout <<"An exception occurred. Exception Nr. "
<< e <<'\n'
; }
return[code] 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 classThe base exception type for all COM interop exceptions and structured exception handling(SEH) exceptions.
[SerializableAttribute][ComVisibleAttribute(true)]
public ref class ExternalException : public SystemExceptionAs the sample above, makes your target clause derived from ExternalException class, then internally the function can capture the exception.
相关文章推荐
- Java Notes-7
- HDOJ 5595 GTW likes math(简单数学,模拟)
- C#5.0之委托
- Drawable Animation 使用介绍
- 使用View Animation实现动画加载界面
- Java Notes-4
- WCDMA笔记(6)
- Java Notes-5
- engine备份与恢复小记
- WindowManager类使用详解——用户首次打开APP的使用教学蒙板效果实现
- View Animation 使用介绍
- Failover cluster configuration common issues
- maven 配置错误。
- 安卓学习笔记四
- python小程序4
- 使用PagerSlidingTabStrip实现滑动标签同步的ViewPager效果
- 通用的桌面悬浮窗口的实现
- 分页查询页码最后跳转框的字符过滤
- 使用ScrollingTabsView实现有滑动标签的ViewPager效果
- 复习下cmake