我想生成和处理异常(SEH)。
如何编写会导致“非法指令”异常的代码?
问问题
240 次
2 回答
1
你可以这样做:
#include <Windows.h>
#include <cstdio>
::DWORD FilterFunction(::DWORD const _exception_code)
{
if(EXCEPTION_ILLEGAL_INSTRUCTION == _exception_code)
{
::std::printf("filtered\n");
return EXCEPTION_EXECUTE_HANDLER;
}
else
{
::std::printf("not filtered\n");
return EXCEPTION_CONTINUE_SEARCH;
}
}
int main()
{
__try
{
::std::printf("trying...\n");
::RaiseException
(
EXCEPTION_ILLEGAL_INSTRUCTION // exception id
, 0 // continuable exception
, 0 // args count
, nullptr // args
); // no arguments
::std::printf("finished trying\n");
}
__except(FilterFunction(GetExceptionCode()))
{
::std::printf("exception handled\n");
}
return 0;
}
试...
过滤
异常处理
于 2018-10-09T13:10:23.220 回答
0
您可以通过以下方式在 Windows 中引发结构化异常:
RaiseException(EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_NONCONTINUABLE, 0, nullptr);
参考:
https://msdn.microsoft.com/en-us/library/ms680552%28VS.85%29.aspx
于 2018-10-09T13:11:54.530 回答