-1

I need help with exceptions , i want to make Exception for each cout, i have this method, and i got a little confused about how to make the exception with the try catch for every if

istream & operator>>(istream & input, Date &date) 
{ 
    int day, mounth, year;
    cout << "please enter day , mounth year" << endl;
    input >> day >> mounth >> year;
    date.setDay(day);
    date.setMonth(mounth);
    date.setYear(year);
    if (day < 1 || day >31)
        throw "Illegal day for month should be number day - between 1 to 31" ;
    if (mounth < 1 || mounth > 12)
        throw "Illegal month should be number mount - between 1 to 12" ;
    if ((mounth == 4 || mounth == 6 || mounth == 9 || mounth == 11)
        && (date.day > 30))
        throw "Illegal day for month " ;
    if (year == 1 && mounth == 1 && day == 1)
        throw "please stop the while loop your date is 1/1/1" ;

        return input;
 }
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
  • its try catch not actch Please go through the https://stackoverflow.com/questions/134569/c-exception-throwing-stdstring – Nagappa Jan 18 '20 at 16:17
  • All of those `throw` statements would be better handled by moving them into the `Date` class methods instead. And what if `input` is not `std::cin`? Prompting the user with `std::cout` wouldn't make sense in that situation. Prompting the user inside an `operator>>` is generally discouraged. – Remy Lebeau Jan 18 '20 at 17:10
  • @ Remy Lebeau , there is option to example hot to do this – danielshmu shmayovich Jan 18 '20 at 17:21

1 Answers1

1

In your code there is no try neither catch blocks, the throw keyword needs to be wrapped into the try block like so:

try {
    std::cout << "Throwing an integer exception...\n";
    throw 42;
} catch (int i) {
    std::cout << " the integer exception was caught, with value: " << i << '\n';
}

This is an example from cppreference try catch page, I recommend you to read this.

For your needs, you'd do something like this:

int main()
{
    int day = 0;
    try {
        std::cin >> day;
        if (day < 1 || day >31)
            throw std::string("Illegal day for month should be number day - between 1 to 31");
    } catch (const std::string &error) {
        std::cout << "Catch error: " << error << "\n";
    }
}

A good way to make well handled exception is by writing your owns. This could help you going through this: Creating custom exceptions

Lucas Gras
  • 961
  • 1
  • 7
  • 22
  • `catch (std::string error)` - I'd argue that exception objects should always be caught by `const` reference, so `catch (const std::string& error)`. – Jesper Juhl Jan 18 '20 at 16:31
  • I did it like this first then edit cause I wasn't sure. Thanks for the comment I edited the answer. – Lucas Gras Jan 18 '20 at 16:34
  • but why when i ran invalid date , not see this error printed "Illegal day for month should be number day - between 1 to 31" – danielshmu shmayovich Jan 18 '20 at 16:41
  • @danielshmushmayovich I just edited my post, you need to throw `std::string` not just `throw "some string"`. This was the mistake, now it's working. – Lucas Gras Jan 19 '20 at 12:24