请问你能检查一下这段代码吗? try/catch/throw 有什么问题?
#include<iostream>
using namespace std;
int get_input();
int main() {
int number, base_in, base_out;
bool pass = 1;
while(pass) {
double number, base_in, base_out;
try {
cout << "What's your number? ";
number = get_input();
pass = 0;
}
catch(problem_type()) {
cout << "Please, write inputs should be integer" << endl;
}
}
return 0;
}
int get_input(bool target = 1) {
double n;
cin >> n;
if(n != (int)n) throw problem_type();
if(target) {
if(n<1) throw problem_type();
}
return (int)n;
}
最佳答案
您按类型捕获。喜欢
catch(const problem_type&){ }
也就是说,如果 problem_type
是类型。我在任何地方都看不到定义......
关于c++ - 在 C++ 中尝试/抛出/捕获 : what's wrong with my code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694504/