int main ( int argc , char *argv[] )
{
const int DefaultSize = 10;
int top = 90;
int bottom = 0;
try
{
cout << "top/2= " << (top/2) << endl;
cout << "top divided by bottom = ";
cout << ( top / bottom ) << endl;
cout << "top/3= 0" << ( top / 3 ) << endl;
}
catch ( ... )
{
cout << "something has gone wrong!" << endl;
}
cout << "Done." << endl;
return 0;
}
这段代码在vs2008sp1默认设置下,程序会崩溃,不会捕获到异常。
默认的异常处理模型是 /EHsc
在 c/c++编译器选项里查看的 /EHs 的含义
可见,vs 默认仅捕获 C++ 异常,将不捕获访问冲突和 System.Exception 异常。(注:SEH 即【结构化异常】或者叫【异步异常】, 而 【c++ 异常】 也就是【同步异常】)
改为: 是,但有SEH异常( /EHa)后,捕获到了 除 0 导致的系统异常。
备注:
使用 /EHs 指定同步异常处理模型(没有结构化异常处理异常的 C++ 异常处理)。如果使用 /EHs,那么 catch 子句将不会捕获异步异常。此外,在 Visual C++ 2005 中,当生成异步异常时,即使处理了异步异常也不会损坏范围内的所有对象。在/EHs 下,catch(...) 仅捕获 C++ 异常。将不捕获访问冲突和 System..::.Exception
异常。
使用 /EHa 指定异步异常处理模型(具有结构化异常处理异常的 C++ 异常处理)。/EHa 可能导致映像性能较差,因为编译器将不会积极地优化 catch 块,即使编译器没有发现 throw 异常。