捕捉异常而不必抛出

时间:2016-12-08 20:10:22

标签: c++ delphi exception try-catch

我是C ++的新手,我来自Delphi场景,即使不必声明显式'' does not exist in current working directory ,我也可以捕获异常。看看这里:

throw

我已经看到一些异常(例如除零)并没有被自动捕获,所以我必须自己创建一个#include<iostream> #include<exception> int main() { try { int c; std::cin >> c; } catch(...) { std::cerr << "Warning!" << std::endl; char xz; std::cin >> xz; return 1; } char z; std::cin >> z; return 0; } //I return 1 because UNIX OS cares about this, a return != 0 means that Huston we have a problem ,这将在我的try-block中被捕获。

如果您查看上面的代码,当我第一次输入throw6.7时,我应该能够在输出test上看到,但没有。我在Windows 10计算机上运行。

我知道warning!是一般的,并且确实给了我保护,但为什么它没有抓错输入?

注意即可。我之前提到过Delphi,因为如果你看下面的代码,我就能发现错误。

catch(...)

为什么我不能用C ++产生相同的效果?我知道他们是两种不同的语言,但起初我会说delphi&#34;对待&#34;例外更好。例如,它会自动捕获除以零(见下文),而c ++不会。

try

 a := StrToInt('6.78');              //conversion error [string->double], a is int
 ShowMessage('a is ' + a.ToString);

except
 on E: Exception do

 ShowMessage('Warning! > ' + e.Message);
 //^ raises "Warning! > '6.78' is not a valid integer value."

end;

结论即可。所以问题是:我是否正确地声明了C ++ //this raises the "Division by zero" error. a := 8 div StrToInt('0'); ?我是否始终必须使用try-catch 确定才能捕获错误,或者我可以省略一些错误?

2 个答案:

答案 0 :(得分:9)

正如documentation中所述,您需要在std::cin上设置异常掩码,使其抛出std::ios_base::failure

#include <iostream>

int main() {
    std::cin.exceptions( std::ios::failbit );
    try {
       int i = 0;
       std::cin >> i;
       std::cout << "i=" << i << std::endl;
    }
    catch(...) {
        std::cerr << "error!" << std::endl;
    }
    return 0;
}

live example

  

我是否总是必须使用throw以确保错误被​​捕获或我可以省略它?

是的,要抛出异常,您需要致电throw。虽然库可能会因为错误条件(包括标准条件)而为您调用throw,但它可能抛出什么异常以及何时应该在文档中说明。

答案 1 :(得分:8)

当用户输入无效类型时,

std::cin不会抛出。

假设C ++ 11及更高版本,行为如下:

  

如果提取失败,则将零写入值并设置failbit。如果   提取导致值太大或太小而无法适应   value,std :: numeric_limits :: max()或std :: numeric_limits :: min()   写入并设置failbit标志。

来自std::basic_istream::operator>>

要检查输入是否无效,您应该执行以下操作:

#include <cstdint>
#include <iostream>

std::int32_t main()
{
    std::int32_t example;
    std::cin >> example;

    if (std::cin.fail())
    {
        std::cout << "Invalid input" << std::endl;
    }
}

或者您也可以这样做:

#include <cstdint>
#include <iostream>

std::int32_t main()
{
    std::int32_t example;

    if (!(std::cin >> example))
    {
        std::cout << "Invalid input" << std::endl;
    }
}