【发布时间】:2021-04-19 17:19:59
【问题描述】:
为什么当我发现错误而 char const* 可以时,字符串在这里不起作用?两者有什么区别?
try {
throw "connection fail";
} catch( string e ) {
if(e == "connection fail") {
std::cout << "caught: " << e << std::endl;
}
}
【问题讨论】:
-
你没有扔
std::string,所以你抓不到。 -
@tkausl 为什么不是字符串?
-
这里不确定,但我怀疑问题是“为什么
catch不遵循与函数参数相同的转换规则?” -
听起来你可以使用good C++ book。 string-literal 是
const char[N],而不是std::string。 -
做
throw "xxxx"s;- 在这里阅读更多en.cppreference.com/w/cpp/string/basic_string/operator%22%22s