vc中异常捕捉的最后一道屏障-SetUnhandledExceptionFilter

 

在C++中定义了很多异常捕捉机制,但是在VC中在win平台上有一个更高层的异常处理机制,函数SetUnhandledExceptionFilter,这个函数很有用,它是异常捕捉的最后一道屏障。它有这样的规则,对于未捕捉的异常并且未在调试状态下(通常就是只在release中),将自动调用这里面的异常处理函数。

默认的异常处理函数就是弹出一个对话框,告诉你程序异常了,而在发布软件时,你可以用其他函数替代,优雅的结束程序。这个处理函数的定义是这样的:

LONG WINAPI myUnhandledExceptionFilter(_EXCEPTION_POINTERS* pExceptionInfo)
{
 ...

return
}

 

这个函数有三种返回值,来表示不同的程序行为:

 

EXCEPTION_EXECUTE_HANDLER
0x1

Return from UnhandledExceptionFilter and execute the associated exception handler. This usually results in process termination.

EXCEPTION_CONTINUE_EXECUTION
0xffffffff

Return from UnhandledExceptionFilter and continue execution from the point of the exception. Note that the filter function is free to modify the continuation state by modifying the exception information supplied through itsLPEXCEPTION_POINTERS parameter.

EXCEPTION_CONTINUE_SEARCH
0x0

Proceed with normal execution of UnhandledExceptionFilter. That means obeying theSetErrorMode flags, or invoking the Application Error pop-up message box.

第一种就是悄悄的终止程序了

第二种就是从这个异常处继续执行

第三种就是调用默认的方法,可能就是弹出一个框,当然这个默认的方法通常是遵循SetErrorMode 的模式。

 

其实标准C++中也有一个类似的函数set_unexpected,区别是,这个函数的执行情况比较有限制,只是当C++ 函数抛出一个不在它自己参数列表中的异常时才会触发,局限性比较大。

posted on 2013-03-08 10:59  leonwei  阅读(420)  评论(0编辑  收藏  举报