Neeraj-Kumar-Coder
Neeraj-Kumar-Coder

Reputation: 405

What does this warning mean in C++ for exception handling?

I have written a code for exception handling for division operation:
I had include the Zero division error, Negative value error (Not an exception but I included it!) and Indeterminate form error (I included it also).

Then after compilation it shows some warnings, but the .exe file is running as expected.
Here is the code and the output which I receive after compilation.

CODE

#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;
}

OUTPUT

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...

What does this warning mean here?
Although the output of the program is as expected for every corner and exceptional cases.

Upvotes: 1

Views: 747

Answers (2)

nhatnq
nhatnq

Reputation: 1193

The invalid_argument is derived from logic_error and the codes are executed from up to down. It is meant the exception will be caught by the logic_error first. The invalid_argument is redundant and could be removed

Upvotes: 2

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122476

A stripped down version of your code that triggers the same warning is this:

#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();
    }
}

Output is

Invalid Arguments: Negative numbers not allowed!

Because std::invalid_argument inherits from std::logic_error and the exception is already handleded by the first catch. If you want to catch both seperately, the general logic_error and the more specialized invalid_argument, you need to do it in opposite order:

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

Upvotes: 5

Related Questions