首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ 標準異常拋出:GCC 與 MSVC 差異

     編輯:關於C++

問題

throw std::exception("msg"); 在 MSVC 可編譯,但在 GCC 4.8.1 報錯:call to 'std::exception::exception(const char [n])'。標準 std::exception 建構函式不接受字串參數。

正確寫法

#include <stdexcept>
// 使用衍生類別
std::logic_error ex("XXX");
throw ex; // 或 throw std::logic_error("XXX");

標準異常類別

  • std::logic_error:邏輯錯誤(domain_error, invalid_argument, length_error, out_of_range)。
  • std::runtime_error:執行期錯誤(range_error, overflow_error, underflow_error)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved