这个警告在 C++ 中对于异常处理意味着什么?
What does this warning mean in C++ for exception handling?
我写了除法运算异常处理的代码:
我包括了 Zero division error
、Negative 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...
这里的警告是什么意思?
尽管对于每个角落和异常情况,程序的输出都符合预期。
触发相同警告的精简代码版本是:
#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();
}
invalid_argument
源自logic_error
,代码从上到下执行。这意味着异常将首先被 logic_error
捕获。 invalid_argument
是多余的,可以删除
我写了除法运算异常处理的代码:
我包括了 Zero division error
、Negative 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...
这里的警告是什么意思?
尽管对于每个角落和异常情况,程序的输出都符合预期。
触发相同警告的精简代码版本是:
#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();
}
invalid_argument
源自logic_error
,代码从上到下执行。这意味着异常将首先被 logic_error
捕获。 invalid_argument
是多余的,可以删除