我的代码只是为了测试。 我尝试检查当类型转换不可能时,stringstream是否会抛出异常。stringstream从double到char,没有失败,没有例外
以下是我的代码。你可以在你的环境中检查它。
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream stream("432.23");
char c = 0;
try {
if(!stream>>c) {
cout<<"Error happend"<<endl;
return 0;
}
}
catch(...) {
cout<<"Exception happend"<<endl;
}
cout<<"c="<<c<<endl;
return 0;
}
输出是 C =^@
不是应该找到错误?