我试图抓住下面的例外情况 -
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
知道我在这里做错了什么?
答案 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/