设为首页 加入收藏

TOP

C、C++异常处理区别(三)
2012-11-28 12:57:37 来源: 作者: 【 】 浏览:1169
Tags:异常 处理 区别

 

    下面这个例子是如何将C异常包装成C++(www.cppentry.com)异常

    [cpp] view plaincopyprint

    // exceptions_Exception_Handling_Differences3.cpp

    // compile with: /EHa

    #include <stdio.h>

    #include <eh.h>

    #include <windows.h>

    class SE_Exception {

    private:

    SE_Exception() {}

    unsigned int nSE;

    public:

    SE_Exception( SE_Exception& e) : nSE(e.nSE) {}

    SE_Exception(unsigned int n) : nSE(n) {}

    ~SE_Exception() {}

    unsigned int getSeNumber() { return nSE; }

    };

    void SEFunc() {

    __try {

    int x, y = 0;

    x = 5 / y;

    }

    __finally {

    printf_s( "In finally\n" );

    }

    }

    void trans_func( unsigned int u, _EXCEPTION_POINTERS* pExp ) {

    printf_s( "In trans_func.\n" );

    throw SE_Exception( u );

    }

    int main() {

    _set_se_translator( trans_func );

    try {

    SEFunc();

    }

    catch( SE_Exception e ) {

    printf_s( "Caught a __try exception with SE_Exception.\n" );

    printf_s( "nSE = 0x%x\n", e.getSeNumber() );

    }

    }

    程序结果:

    In trans_func.

    In finally

    Caught a __try exception with SE_Exception.

    nSE = 0xc0000094

    综上,C异常仅能被try{}catch(…){}来捕获处理,但是并没有关于异常类型和内容的说明,为了异常更清楚,可以将C异常包装成C++(www.cppentry.com)异常(上述例子)。

      

首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇GNU,gcc,g++,gdb,cc概念 下一篇C/C++ 数据范围int

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: