本文介绍了为什么不让我的程序执行第二个catch块,当错误存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的 try / catch 异常处理,我想知道为什么我的第二个 catch sec 变量不应该在0-59之间,所以我想说无效的第二个条目,但它不是。谢谢!

I am new to try/catch exception handling, and am wondering why my second catch block will not execute. The sec variable should not be between 0-59, so I'd like it to say "invalid second entry", but it doesn't. Thank you!

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


推荐答案

。在这种情况下,下一个执行的块将是你的 catch(const BadHourError&),因此 Object.getSec()永远不会被评估。你的处理是正确的,它会 throw 但不是如果你的第一个如果语句 throw

throw will return control to the next matching exception handler. In this case, the next block executed will be your catch (const BadHourError &), so Object.getSec() is never even evaluated. Your handling here is correct, and it will throw but not if your first if statement throw instead.

您可以改为:

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()> 59 if getHour()> 2359

Now they will be handled separately from each other, ensuring they both get tested; however, you need to decide whether it's worth testing both. If an hour is invalid, what does it matter if everything is correct or invalid? Your class probably can't function well, so it doesn't matter if getSec() > 59 if getHour() > 2359

这篇关于为什么不让我的程序执行第二个catch块,当错误存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:24