【发布时间】:2020-01-18 16:15:09
【问题描述】:
我需要异常方面的帮助,我想为每个cout 生成异常,我有这个方法,但我有点困惑如何使用每个 if 的 try catch 来生成异常
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;
}
【问题讨论】:
-
它的try catch not actch 请通过stackoverflow.com/questions/134569/…
-
所有这些
throw语句可以通过将它们移到Date类方法中得到更好的处理。如果input不是std::cin怎么办?在那种情况下,用std::cout提示用户是没有意义的。通常不鼓励在operator>>内提示用户。 -
@Remy Lebeau ,可以选择例如热来执行此操作
标签: c++