VS配置将崩溃转化为异常

   相关资料链接:http://msdn.microsoft.com/zh-cn/library/1deeycx5%28v=VS.80%29.aspx

    在调用系统API或其他的异常引起程序崩溃终止,可以在VS的工程配置异常选项,将引起程序崩溃的异常转化为用try_catch可以处理的异常。

    设置位置 Project Property Pages-> Configuration Properties -> C/C++->Code Generation-> Enable C++ Exceptions -> Yes with SEH Exception (/EHa)

 

异常捕获含义:

    EHa  捕获异步(结构化)异常和同步 (C++) 异常的异常处理模型。
    EHs  仅捕获 C++ 异常并通知编译器假定 extern C 函数确实引发了异常的异常处理模型。
    EHc    如果与 s ( /EHsc) 一起使用,则仅捕获 C++ 异常并通知编译器假定 extern C 函数从未引发 C++ 异常。 /EHca 等效于 /EHa

同时也可以使用Windows的API函数 _set_se_translator,捕获异常后进行处理

相关链接:http://technet.microsoft.com/zh-cn/subscriptions/5z4bw5h5%28v=vs.71%29.aspx

_set_se_translator

Handles Win32 exceptions (C structured exceptions) as C++ typed exceptions.

_se_translator_function _set_se_translator(
   _se_translator_function se_trans_func 
);
Parameter
se_trans_func
Pointer to a C structured exception translator function that you write.
Return Value

Returns a pointer to the previous translator function registered by _set_se_translator, so that the previous function can be restored later. If no previous function has been set, the return value may be used to restore the default behavior; this value may be NULL.

Remarks

The _set_se_translator function provides a way to handle Win32 exceptions (C structured exceptions) as C++ typed exceptions. To allow each C exception to be handled by a C++catch handler, first define a C exception wrapper class that can be used, or derived from, to attribute a specific class type to a C exception. To use this class, install a custom C exception translator function that is called by the internal exception-handling mechanism each time a C exception is raised. Within your translator function, you can throw any typed exception that can be caught by a matching C++catch handler.

Use /EHa instead of /EHsc when using _set_se_translator.

To specify a custom translation function, call _set_se_translator with the name of your translation function as its argument. The translator function that you write is called once for each function invocation on the stack that hastry blocks. There is no default translator function.

In a multithreaded environment, translator functions are maintained separately for each thread. Each new thread needs to install its own translator function. Thus, each thread is in charge of its own translation handling._set_se_translator is specific to one thread; another DLL can install a different translation function.

The se_trans_func function that you write must take an unsigned integer and a pointer to a Win32_EXCEPTION_POINTERS structure as arguments. The arguments are the return values of calls to the Win32 APIGetExceptionCode and GetExceptionInformation functions, respectively.


typedef void (*_se_translator_function)(unsigned int, struct _EXCEPTION_POINTERS* );

For _set_se_translator, there are implications when dynamically linking to the CRT; another DLL in the process may call_set_se_translator and replace your handler with its own.

Requirements
RoutineRequired headerCompatibility
_set_se_translator<eh.h>Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_settrans.cpp
// compile with: /EHsc
#include <stdio.h>
#include <windows.h>
#include <eh.h>

void SEFunc();
void trans_func( unsigned int, EXCEPTION_POINTERS* );

class SE_Exception
{
private:
    unsigned int nSE;
public:
    SE_Exception() {}
    SE_Exception( unsigned int n ) : nSE( n ) {}
    ~SE_Exception() {}
    unsigned int getSeNumber() { return nSE; }
};
int main( void )
{
    try
    {
        _set_se_translator( trans_func );
        SEFunc();
    }
    catch( SE_Exception e )
    {
        printf( "Caught a __try exception with SE_Exception.\n" );
    }
}
void SEFunc()
{
    __try
    {
        int x, y=0;
        x = 5 / y;
    }
    __finally
    {
        printf( "In finally\n" );
    }
}
void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    printf( "In trans_func.\n" );
    throw SE_Exception();
}
Output
In trans_func.In finallyCaught a __try exception with SE_Exception.



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值