c++ - 在 C++ 中尝试/抛出/捕获 : what's wrong with my code?

标签 c++ try-catch

请问你能检查一下这段代码吗? 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/

相关文章:

c++ - 如何对 float 执行按位运算

java - 当 catch 并没有真正捕捉到任何东西时

java - 带异常处理的 while 循环

Swift 4/Xcode 9.3/iOS - 捕获文件 "x"无法打开,因为没有带有 String(contentsOf : url)

c++ - 使用可变修改类型实例化模板

c++ - 使用递归反向链表

c++ - 如何在 C++ 中跳过读取文件中的一行?

c++ - 如何: return multidimensional vector (Matrix) from function - using header

java - 未报告的异常......必须捕获或声明抛出

java - 正在捕获 Java Throwable 重新启动线程的最佳方法