捕获异常而不必抛出 我是C++的新手,我来自Delphi场景,在这个过程中,即使没有声明一个明确的代码>抛出< />代码,我也能捕获异常。看看这里: #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 为什么我不能用C++产生同样的效果?我知道它们是两种不同的语言,但一开始我会说delphi更好地“处理”异常。例如,它自动地除以零(参见下面),而C++没有。 //this raises the "Division by zero" error. a := 8 div StrToInt('0');

捕获异常而不必抛出 我是C++的新手,我来自Delphi场景,在这个过程中,即使没有声明一个明确的代码>抛出< />代码,我也能捕获异常。看看这里: #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 为什么我不能用C++产生同样的效果?我知道它们是两种不同的语言,但一开始我会说delphi更好地“处理”异常。例如,它自动地除以零(参见下面),而C++没有。 //this raises the "Division by zero" error. a := 8 div StrToInt('0');,c++,delphi,exception,try-catch,C++,Delphi,Exception,Try Catch,结论。所以问题是:我声明C++ >代码>正确地捕获catch 吗?我是否必须始终使用throw来确保捕获错误,或者我可以在某些时候忽略它?std::cin在用户输入无效类型时不会抛出 假设C++11及更高版本,行为如下: #include <cstdint> #include <iostream> std::int32_t main() { std::int32_t example; std::cin >> example; if

结论。所以问题是:我声明C++ >代码>正确地捕获catch <代码>吗?我是否必须始终使用
throw
确保捕获错误,或者我可以在某些时候忽略它?

std::cin
在用户输入无效类型时不会抛出

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

#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;
    }
}
若提取失败,则将零写入值并设置failbit。如果 提取会导致值太大或太小而无法容纳 值,std::numeric_limits::max()或std::numeric_limits::min() 写入,并设置故障位标志

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

#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;
    }
}
#包括
#包括
std::int32_t main()
{
std::int32\t示例;
std::cin>>示例;
if(std::cin.fail())
{
std::cout(示例)
{

当用户输入无效类型时,std::cout
std::cin
不会抛出

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

#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;
    }
}
若提取失败,则将零写入值并设置failbit。若 提取会导致值太大或太小而无法容纳 值,std::numeric_limits::max()或std::numeric_limits::min() 写入,并设置故障位标志

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

#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;
    }
}
#包括
#包括
std::int32_t main()
{
std::int32\t示例;
std::cin>>示例;
if(std::cin.fail())
{
std::cout(示例)
{
std::cout如中所述,您需要在
std::cin
上设置异常掩码以使其抛出:

#包括
int main(){
std::cin.exceptions(std::ios::failbit);
试一试{
int i=0;
标准:cin>>i;
std::cout如中所述,您需要在
std::cin
上设置异常掩码以使其抛出:

#包括
int main(){
std::cin.exceptions(std::ios::failbit);
试一试{
int i=0;
标准:cin>>i;

STD:CUT catch是一个不规则动词,第二个是被抓获,不是抓到。(捕获,捕获,捕获)异常在Delphi中与C++中的工作大致相同。不能捕捉什么是不被抛出的。有人必须抛出,不管是你还是你所调用的库。
有显式的
raise
语句,在遇到无效输入数据时抛出异常。Delphi RTL还响应系统生成的异常(访问冲突、被零除等)也就是说,C++可以不做那种转换,但是有第三个库,catch是不规则动词,第二个是被抓到的,不是抓到的。在Delphi中,异常与C++中的工作原理大致相同。您无法捕获未被抛出的东西。必须抛出,无论是您还是您所调用的库。
有显式的
raise
语句,在遇到无效输入数据时抛出异常。Delphi RTL还响应系统生成的异常(访问冲突、被零除等)这就是为什么你可以捕捉到诸如“代码> EccAccess Buffue/Cube >,<代码> EdidiBy0等。C++不做那个转换,但有第三方库。你也可以使用<代码>布尔O/CUT>转换运算符,例如:<代码>如果((STD::CIN >例子))
。您也可以使用
bool
转换运算符,例如:
if(!(std::cin>>示例))