I have this simple try catch for throwing cin exceptions, but it never throws exceptions.
while(cin>>num) {
try {
if(cin.fail()) {
throw "error";
}
if(num>0) {
cout << "number greater than 0" << endl;
}
}
catch(char* error) {
cout << error << endl;
}
}
Why it is not throwing the exception?
cin >> num return false, so your loop body doesn’t get executed at all.
If you really need to use execption
It’s better to put the try catch outside of the loop to get better performance