【问题标题】:What does this warning mean in C++ for exception handling?这个警告在 C++ 中对异常处理意味着什么?
【发布时间】:2021-09-30 08:36:44
【问题描述】:

我写了除法运算的异常处理代码:
我包含了Zero division errorNegative value error(不是例外,但我包含了它!)和Indeterminate form error(我也包含了它)。

然后在编译后它会显示一些警告,但.exe 文件正在按预期运行。
这是我编译后收到的代码和输出。

代码

#include <iostream>
#include <stdexcept>

using namespace std;

int main(void)
{
    int numerator, denominator, quotient, remainder;
    cout << "Enter the value of numerator and denominator: ";
    cin >> numerator >> denominator;
    try
    {
        if (!numerator && !denominator)
        {
            throw logic_error("Logical Error: Indeterminate Form!\n");
        }
        else if (!denominator)
        {
            throw runtime_error("Math Error: Attemp to divide by zero!\n");
        }

        else if (numerator < 0 || denominator < 0)
        {
            throw invalid_argument("Invalid Arguments: Negative numbers not allowed!\n");
        }
        else
        {
            quotient = numerator / denominator;
            remainder = numerator % denominator;
            cout << "The result after division is:\n"
                 << "Quotient: " << quotient << "\nRemainder: " << remainder << '\n';
        }
    }
    catch (logic_error &exc)
    {
        cout << exc.what();
    }
    catch (runtime_error &exc)
    {
        cout << exc.what();
    }
    catch (invalid_argument &exc)
    {
        cout << exc.what();
    }
    catch (...)
    {
        cout << "Some Exception Occured!\n";
    }

    cout << "\nProgram Finished...\n";
    return 0;
}

输出

Exceptional_Handling_05.cpp: In function 'int main()':
Exceptional_Handling_05.cpp:42:5: warning: exception of type 'std::invalid_argument' will be caught
   42 |     catch (invalid_argument &exc)
      |     ^~~~~
Exceptional_Handling_05.cpp:34:5: warning:    by earlier handler for 'std::logic_error'
   34 |     catch (logic_error &exc)
      |     ^~~~~
Enter the value of numerator and denominator: 52 0
Math Error: Attemp to divide by zero!

Program Finished...

这个警告在这里意味着什么?
尽管程序的输出在每个角落和例外情况下都符合预期。

【问题讨论】:

  • 错误信息其实很清楚。 invalid_argument 不需要 catch,因为它继承自 logic_error,因此已经被捕获。在catch (invalid_argument &amp;exc)里面加一些std::cout或者类似的,看看就不会触发了
  • 顺便说一句,尽量少用异常,用于异常情况,而不是控制流。用户输入错误的输入并不是那么特殊。您也可以在检查条件的地方打印消息。但是,我想这是一个关于异常的练习,因此删除它们将毫无意义
  • @463035818_is_not_a_number 是的,我现在正在学习 C++ 中的异常处理。这不是应该做的程序,我只是在练习。
  • 顺便说一句,它不称为“异常处理”
  • 哦,好的,感谢您的编辑 :-)

标签: c++ exception try-catch


【解决方案1】:

invalid_argument 派生自logic_error,代码从上到下执行。这意味着异常将首先被logic_error 捕获。 invalid_argument 是多余的,可以删除

【讨论】:

    【解决方案2】:

    触发相同警告的精简版代码如下:

    #include <iostream>
    #include <stdexcept>
    
    int main()
    {
        int numerator = -1, denominator = -1;
        try
        {        
            if (numerator < 0 || denominator < 0)
            {
                throw std::invalid_argument("Invalid Arguments: Negative numbers not allowed!\n");
            }
        }
        catch (std::logic_error &exc)
        {
            std::cout << exc.what();
        }
        catch (std::invalid_argument &exc)
        {
            std::cout << " THIS IS NEVER REACHED !!";
            std::cout << exc.what();
        }
    }
    

    输出是

    Invalid Arguments: Negative numbers not allowed!
    

    因为std::invalid_argument 继承自std::logic_error 并且异常已由第一个catch 处理。如果您想分别捕获两者,一般的logic_error 和更专业的invalid_argument,您需要按相反的顺序进行:

    catch (std::invalid_argument &exc)
    {
        std::cout << exc.what();
    }
    catch (std::logic_error &exc)
    {
        std::cout << exc.what();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 2012-05-29
      • 2023-01-03
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多