并且用同样的多态函数来获取异常的类型:
catch (std::exception const &exception)
{
cout << "caught " << exception.what();
}
用这样的方案,seh异常能够表现得与标准c++的固有行为一致。同时,我们仍然能够特殊对待structured_exceptions并访问它的特殊成员:
catch (structured_exception const &exception)
{
cout << "caught structured exception from " << exception.where();
}
当然,如果你想放弃没有出现在std::exception继承体系中的类成员,如where(),你完全可以不使用基类structured_exception,而是直接从std::exception继承出access_violation等类。例如:一个divide-by-zero异常表示了一个程序值域控制错误,也就是说是个逻辑错误。你所以想直接从std::logic_error甚至是std::out_of_range派生devide_by_zero类。
我建议你看一下c++标准subclause 19.1 (“exception classes”)以更好地理解c++标准运行库的异常继承体系,以及如何更好地将你的自定义异常熔入此继承体系。