首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >抛出并捕获std::string

抛出并捕获std::string
EN

Stack Overflow用户
提问于 2014-03-20 15:27:52
回答 3查看 2.8K关注 0票数 4

我写了一些奇怪的代码,但令人惊讶的是它还能工作。但现在我不知道我抛出了什么,我怎么才能接住它:

代码语言:javascript
复制
class Date {
private:
    int day;
    int month;
    int year;
    int daysPerMonth[];
public:
    Date( int day, int month, int year ) {
        int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

        if(isValidDate(day, month, year)) {
            this->day = day;
            this->month = month;
            this->year = year;
        } else {
            throw std::string("No valid input date numbers...");//what i throw???
        }
    }

请帮我写代码。

EN

Stack Overflow用户

发布于 2014-03-20 15:49:39

你可以像下面这样使用(未编译):

代码语言:javascript
复制
class Date {
private:
int day;
int month;
int year;
int daysPerMonth[];
public:
Date( int day, int month, int year ) {
    int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

    if(isValidDate(day, month, year)) 
    {
        this->day = day;
        this->month = month;
        this->year = year;
    } else {
        throw std::string("No valid input date numbers...");//what i throw???
        }
    }
};

int main()
{
 try{
    Date d(55,223,0122);
  }
 catch(std::string &e)
 {
    // Do necessary handling
  }
}
票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22526198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档