sha*_*hah 5 c++ exception-handling
float input;
cin>>input; // if the user type string in input then throw exception
if(!isdigit(input)){
throw "error";
}
Run Code Online (Sandbox Code Playgroud)
但isdigit也会为数值抛出异常.
怎么解决?
float input;
if (cin>>input) {
//all is good
...
} else {
throw "error";
}
Run Code Online (Sandbox Code Playgroud)
是一种方法.if如果输入以数字和else路径开头,程序将采用路径.