catch块让我无法匹配操作员错误

时间:2013-10-16 17:45:25

标签: c++ exception stl try-catch overloading

我试图抓住下面的例外情况 -

try {

} catch (const std::exception& ex) {
    cout << "An exception occurred when executing query. " << ex << endl;
}

但每次我收到此错误 -

no match for operator<< in std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"An exception occurred when executing query. ")) << ex

知道我在这里做错了什么?

3 个答案:

答案 0 :(得分:3)

使用此

cout << "An exception occurred when executing query. " << ex.what() << endl;

<<类中没有exception运算符重载。

答案 1 :(得分:1)

编译器(尝试)告诉您未声明operator<<(std::ostream&, std::exception const&)

答案 2 :(得分:1)

匹配<<的运算符std::exception没有重载。请改用ex.what()what()会返回char* <<理解的{{1}}。 参考:http://www.cplusplus.com/reference/exception/exception/what/