编辑:我的意思不是 SEH,而是异步异常处理。正如所评论的,SEH 由 __try、__except 支持。以下示例需要异步异常处理 (-EHa),但该处理似乎不可用。
我在这里尝试了微软给出的结构化异常处理(SEH)示例:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/set-se-translator?view=msvc-170
转载如下,删除#pragmas(我的版本仅是本机版本)。
它可以与 Microsoft 编译器 (cl) 和 Intel 经典编译器 (icl) 配合使用,并且已经这样做了一两年了。
clang-cl(版本 14.0.5)和 Intel LLVM 编译器(icx)失败。函数 my_trans_func 不会被调用。
我按照指示使用了 -EHa 开关,没有一个编译器抱怨它。
这引出了两个问题:
// crt_set_se_translator_clr.cpp
// compile with: cl /W4 /clr crt_set_se_translator_clr.cpp
#include <windows.h>
#include <eh.h>
#include <stdio.h>
#include <exception>
int thrower_func( int i ) {
int y = 0;
int *p = &y;
*p = i / *p;
return 0;
}
class SE_Exception : public std::exception
{
private:
const unsigned int nSE;
public:
SE_Exception() noexcept : SE_Exception{ 0 } {}
SE_Exception( unsigned int n ) noexcept : nSE{ n } {}
unsigned int getSeNumber() const noexcept { return nSE; }
};
class Scoped_SE_Translator
{
private:
const _se_translator_function old_SE_translator;
public:
Scoped_SE_Translator( _se_translator_function new_SE_translator ) noexcept
: old_SE_translator{ _set_se_translator( new_SE_translator ) } {}
~Scoped_SE_Translator() noexcept { _set_se_translator( old_SE_translator ); }
};
void my_trans_func( unsigned int u, PEXCEPTION_POINTERS )
{
throw SE_Exception( u );
}
void DoTest()
{
try
{
thrower_func( 10 );
}
catch( const SE_Exception& e )
{
printf( "Caught SE_Exception, error %8.8x\n", e.getSeNumber() );
}
catch(...)
{
printf( "Caught unexpected SEH exception.\n" );
}
}
int main() {
Scoped_SE_Translator scoped_se_translator{ my_trans_func };
DoTest();
}
此问题已计划在 oneAPI 2024.0 版本中修复,该版本将在未来几个月发布。
您可以将 /EHa 标志用于具有 /Od 优化的英特尔 oneAPI C++ 编译器 (icx)。编译器将能够处理此异常。
如果新版本中问题仍然存在,那么您可以开始新的讨论以供社区进行调查。