【问题标题】:Why wont my program execute the second catch block, when the error exists?当错误存在时,为什么我的程序不会执行第二个 catch 块?
【发布时间】:2016-03-29 05:18:12
【问题描述】:

我是try/catch 异常处理的新手,我想知道为什么我的第二个catch 块不会执行。 sec 变量不应介于 0-59 之间,因此我希望它说“无效的第二个条目”,但事实并非如此。谢谢!

#include <stdexcept>
#include <iostream>
#include <string>

using namespace std;


class BadHourError : public runtime_error
{
    public:
    BadHourError() : runtime_error("") {}
};

class BadSecondsError : public runtime_error
{
    public:
    BadSecondsError() : runtime_error("") {}
};

class Time
{
protected:
    int hour;
    int min;
    int sec;
public:
    Time()
    {
        hour = 0; min = 0; sec = 0;
    }

    Time(int h, int m, int s)
    {
        hour = h, min = m, sec = s;
    }

    int getHour() const
    {return hour;}

    int getMin() const
    {return min;}

    int getSec() const
    {return sec;}
};

class MilTime : public Time
{
protected:
    int milHours;
    int milSeconds;

public:
    MilTime() : Time()
    {
    setTime(2400, 60);
    }

    MilTime(int mh, int ms, int h, int m, int s) : Time(h, m, s)
    {
    milHours = mh;
    milSeconds = ms;
    getHour();
    getMin();
    getSec();
    }

    void setTime(int, int);
    int getHour(); //military hour
    int getStandHr(); 
};

void MilTime::setTime(int mh, int ms)
{
milHours = mh;
milSeconds = ms;
sec = milSeconds;
getSec();
}

int MilTime::getHour()
{
return milHours;
}

int MilTime::getStandHr()
{
return hour;
}


int main()
{
MilTime Object;

try
{
if ( (Object.getHour() < 0) || (Object.getHour() > 2359) ) throw BadHourError();
if ( (Object.getSec()  < 0) || (Object.getSec()  > 59  ) ) throw BadSecondsError();
}

catch (const BadHourError &)
{
cout << "ERROR, INVALID HOUR ENTRY";
}

catch (const BadSecondsError &)
{
cout << "ERROR, INVALID SECOND ENTRY";
}
return 0;
}

【问题讨论】:

    标签: c++ exception exception-handling try-catch


    【解决方案1】:

    throw will return control to the next matching exception handler。在这种情况下,执行的下一个块将是您的catch (const BadHourError &amp;),因此甚至永远不会评估Object.getSec()。您在此处的处理是正确的,它会 throw 但如果您的第一个 if 声明 throw 而不是。

    你可以这样做:

    try
    {
        if ( (Object.getHour() < 0) || (Object.getHour() > 2359) )
           throw BadHourError();
    }
    catch (const BadHourError &)
    {
        cout << "ERROR, INVALID HOUR ENTRY";
    }
    
    try
    {
        if ( (Object.getSec()  < 0) || (Object.getSec()  > 59  ) )
            throw BadSecondsError();
    }
    catch (const BadSecondsError &)
    {
        cout << "ERROR, INVALID SECOND ENTRY";
    }
    

    现在它们将彼此分开处理,确保它们都得到测试;但是,您需要决定是否值得同时测试两者。如果一个小时是无效的,那么一切正确或无效有什么关系?你的类可能不能正常运行,所以getSec() &gt; 59getHour() &gt; 2359 没关系

    【讨论】:

    • 请注意,在这个简单的程序中,您应该只使用if ( (Object.getSec() &lt; 0) || (Object.getSec() &gt; 59 ) ) cout &lt;&lt; "ERROR, INVALID SECOND ENTRY";,除非您正在学习 try/catch 的工作原理。
    【解决方案2】:

    因为你的 milHour 是 2400,所以在糟糕的时间会抛出异常。

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2019-08-20
      • 1970-01-01
      • 2021-09-04
      • 2020-12-11
      • 2022-01-23
      • 2021-06-18
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多