我有這個簡單的try catch來拋出cin異常,但它從不拋出異常。cin和try/catch
while(cin>>num) {
try {
if(cin.fail()) {
throw "error";
}
if(num>0) {
cout << "number greater than 0" << endl;
}
}
catch(char* error) {
cout << error << endl;
}
}
爲什麼它不拋出異常?
這可能是不相關的,但你應該打印錯誤'cerr' ,而不是'cout'。 –
哦,順便說一下。請不要拋出那些不是來自std :: exception的東西。 –