尝试/捕获崩溃255

时间:2013-11-08 18:53:40

标签: c++

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 个答案:

答案 0 :(得分:8)

你确定这个编译?你在复制粘贴中省略了什么吗? getmsg()方法不会返回任何内容。

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

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

答案 1 :(得分:1)

你的代码中有很多不好的语法。您得到的错误是因为您使用返回值声明getMsg并且不返回(UB - 您很幸运它甚至编译!)。

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

答案 2 :(得分:1)

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

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

应该是:

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

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

暂时搁置这些错误,从设计的角度来看,从一个异常基类继承是更清晰的。我通常会继承std::runtime_error,但你可以根据需要定义自己的。{1}}。例如:

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

答案 3 :(得分: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);
}