閱讀544 返回首頁    go 阿裏雲 go 技術社區[雲棲]


VS 2017 C++使用 try

手頭有本C++基礎課本,“The Programming Principles and Practice Using C++" ,以Visual Studio 2005 為背景講述,在講到關於代碼中的 ”error and debugging"中,使用了如下示例代碼:

#include "../../std_lib_facilities.h"   // 書本給出的頭文件,可以完成基本功能
int main()
try {
string s = "Success!\n";              //長度為9的字符串
for (int i=0; i<10; ++i)               // exception 所在,讀取字符串 越界
cout << s[i];
keep_window_open();               
return 0;
}
catch (exception& e) {
cerr << "error: " << e.what() << '\n';          // 輸出錯誤信息
keep_window_open();                                 //阻止運行窗口關閉
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open();
return 2;
}

其在 VS 2005 上的結果為:

Success! 

error: Range error: 9

……

而在VS 2017 上的結果:

Success!

(直接跳轉到開發環境中,打開相關的底層代碼(與string相關),並在旁邊使用“窗口標簽”提示,代碼出錯及原因)

請問這是什麼原因呢?如何使VS 2017上的運行情況和VS 2005上相同呢?



最後更新:2017-08-25 16:02:25

  上一篇:go 正確登錄密碼登錄時顯示錯誤
  下一篇:go 為何微軟鼠標和鍵盤中心無法識別出Sculpt舒適滑控鼠標和舒適曲線鍵盤3000?