我有一个 Visual Studio 2008 C++ 程序,该程序包含在__try
/__except
块中以捕获任何 SEH 异常。异常过滤器会创建错误日志并为用户提供有关如何提交缺陷报告的详细说明。
过滤器中的代码是否需要包装在另一个__try
/__except
块中?如果不是,如果它例外会发生什么?如果有,应该如何处理?
static int MyFilter( struct _EXCEPTION_POINTERS* ep )
{
/*
Code to log the exception information, and instruct the user
on how to submit a defect report. Should this be in another
__try/__except block?
*/
return EXCEPTION_EXECUTE_HANDLER;
}
int WINAPI _tWinMain( HINSTANCE hInstance,
HINSTANCE /*hPrevInstance*/,
LPTSTR lpstrCmdLine,
int nCmdShow )
{
int result = 0;
__try
{
result = Execute( hInstance, lpstrCmdLine, nCmdShow );
}
__except( MyFilter( GetExceptionInformation() ) )
{
// empty
}
return 0;
}
谢谢,保罗
编辑:
如果MyFilter
引发异常,那么我将进入无限异常循环。所以,看起来它确实需要__try
/__except
处理。我正在考虑这样做:
static int MyFilter( struct _EXCEPTION_POINTERS* ep )
{
__try
{
/*
Code to log the exception information, and instruct the user
on how to submit a defect report.
*/
// cause an exception
int x = 0, y = 1 / x;
}
__except( EXCEPTION_EXECUTE_HANDLER ) { /*empty*/ }
return EXCEPTION_EXECUTE_HANDLER;
}
在这种情况下,程序应该有一个异常终止,并且应该将异常传递给操作系统来处理。那是对的吗?