尝试/捕获崩溃 255

Try/catch crash 255

本文关键字:崩溃 尝试      更新时间:2023-10-16

尝试/捕获的新手,需要将其用于程序。出于某种原因,尽管我的程序在捕获 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);
}

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

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

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

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

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

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

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

代码的潜在修复:

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