带有连接消息的c ++运行时错误

时间:2016-03-18 21:18:29

标签: c++ c++11 exception-handling

我想在加载包含其路径

的位图时抛出错误
ALLEGRO_BITMAP* bitmap;
bitmap_path
if(bitmap=al_load_bitmap(bitmap_path)==0){
   throw runtime_error("error loading bitmap from: '"<<bitmap_path<<"'");
};
//continue if no error

1 个答案:

答案 0 :(得分:1)

您无法使用<<运算符直接连接字符串。

如果bitmap_pathstd:::string,请改用+运算符:

throw runtime_error("error loading bitmap from: '" + bitmap_path + "'");

如果bitmap_pathchar*,请将其或第一个字符串文字投放到临时std::string,以便您可以使用+

throw runtime_error("error loading bitmap from: '" + string(bitmap_path) + "'");

throw runtime_error(string("error loading bitmap from: '") + bitmap_path + "'");

否则,您可以使用std::ostringstream或等效值构建临时std::string值:

ostringstream oss;
oss << "error loading bitmap from: '" << bitmap_path << "'";
throw runtime_error(oss.str());