1

try/catch 的新手,需要将其用于程序。出于某种原因,尽管我的程序在捕获 obj 时崩溃了。我写的示例程序只是读入一个数字,然后测试它是否低于 10 或高于 20。关键是使用两个用户定义的类来抛出和捕获。上面的 20 个 obj 被抛出并捕获得很好。但是下面的 10,如果它抛出将导致程序崩溃。为什么是这样?这是我的代码

#include <iostream>
#include <cstdlib>

using namespace std;

class above20
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        };
        string getmsg()
        {
            cout<<msg<<endl;
        };

    private:
        string msg;
};


class below10
{
    public:
        below10()
        {
            msg = "Number is below 10";
        };
        string getmsg()
        {
            cout<<msg<<endl;
        };

    private:
        string msg;
};

int main ()
{
    int num;
    try
    {
        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20 ();
        }
    }
    catch (above20 obj)
    {
        obj.getmsg();
    }

    try
    {
        if (num < 10)
        {
            throw below10 ();
        }   
    }
    catch (below10 obj)
    {
        obj.getmsg();
    }

    system ("pause");
    return (0);
}
4

4 回答 4

8

你确定这编译?您是否在复制粘贴中遗漏了某些内容?getmsg() 方法不返回任何内容。

---编辑---试试这个:

 void getmsg()
 {
        cout<<msg<<endl;
 };
于 2013-11-08T18:57:45.883 回答
1

您的代码中有很多糟糕的语法。你得到的错误是因为你声明getMsg了一个返回值并且没有返回(UB - 你很幸运它甚至可以编译!)。

要解决您的所有问题:http: //ideone.com/1qGAuR

于 2013-11-08T19:07:01.660 回答
1

您的代码中有一些错误,例如有一个不返回任何内容的函数,尽管它应该在签名中:

    string getmsg()
    {
        cout<<msg<<endl;
    };

真的应该是:

    void getmsg()
    {
        cout<<msg<<endl;
    };

或者

    string getmsg()
    {
        return string(msg);
    };

暂时搁置这些错误,从设计的角度来看,从一个异常基类继承更干净。我通常会继承自,std::runtime_error但如果你愿意,你可以定义自己的。例如 :

#include <iostream>
#include <cstdlib>

using namespace std;

class above_below_exception
{ 
    public:
        virtual string getmsg() =0;
};

class above20 : public above_below_exception
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        };
        string getmsg()
        {
            return string(msg);
        };
    private:
        string msg;
};


class below10 : public above_below_exception
{
    public:
        below10()
        {
            msg = "Number is below 10";
        };
        string getmsg()
        {
            return string(msg);
        };
    private:
        string msg;

};

int main ()
{
    int num;
    try
    {
        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20();
        }
        if (num < 10)
        {
            throw below10();
        }
    }
    catch (above_below_exception& obj)
    {
        cout << obj.getmsg();
    }

return (0);
}
于 2013-11-08T19:08:10.007 回答
1

您的代码的潜在修复:

#include <iostream>
#include <cstdlib>

using namespace std;

class above20 : public std::exception
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        }

        virtual ~above20 () throw ()
        {

        }

        string getmsg ()
        {
            cout << msg << endl;
            return msg;
        }

    private:
        string msg;

};


class below10 : public std::exception
{
    public:
        below10()
        {
            msg = "Number is below 10";
        }

        virtual ~below10 () throw ()
        {

        }

        string getmsg()
        {
            cout << msg << endl;
            return msg;
        }

    private:
        string msg;

};

int main ()
{
    int num;
    try
    {

        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20 ();
        }
    }
    catch (above20 &obj)
    {
        cout << obj. getmsg () << endl;
    }

    try
    {
        if (num < 10)
        {
            throw below10 ();
        }
    }
    catch (below10 obj)
    {
        obj.getmsg();
    }


system ("pause");
return (0);
}
于 2013-11-08T19:15:59.587 回答