简体   繁体   English

尝试/捕获崩溃255

[英]Try/catch crash 255

new to the try/catch thing and need to use it for a program. 是try / catch的新手,需要在程序中使用它。 For some reason though my program crashes when it catches the obj. 由于某种原因,尽管我的程序在捕获obj时崩溃了。 The sample program I wrote just reads in a number and then tests to see if its below 10 or above 20. The point is to use two user defined classes to throw and catch. 我编写的示例程序只是读取一个数字,然后进行测试以查看其是否小于10或大于20。关键是要使用两个用户定义的类进行抛出和捕获。 The above 20 obj gets thrown and catches just fine. 上面的20个obj被抛出并捕获就好了。 But the below 10, if its thrown will crash the program. 但是如果低于10,则将其抛出崩溃。 Why is this? 为什么是这样? here is my code 这是我的代码

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

Are you sure this compiles? 您确定会编译吗? Did you omit something in your copy paste? 您是否在复制粘贴中遗漏了某些内容? The getmsg() methods don't return anything. getmsg()方法不返回任何内容。

---edit --- Try this: ---编辑---试试这个:

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

You have a lot of poor syntax in your code. 您的代码中语法很多。 The error you get is because you declare getMsg with a return value and do not return (UB - you are lucky it even compiles!). 您得到的错误是因为您用返回值声明getMsg而不返回(UB-您很幸运它甚至可以编译!)。

To fix all of your issues: http://ideone.com/1qGAuR 要解决所有问题,请访问: http : //ideone.com/1qGAuR

You have a few errors in your code like having a function that doesn't return anything despite saying that it should in the signature: 您的代码中有一些错误,例如,尽管说应该在签名中包含一个不返回任何内容的函数:

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

should really be: 确实应该是:

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

or 要么

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

Putting aside those bugs for a second, from a design point of view inheriting from one exception base class is cleaner. 从设计的角度来看,将这些错误搁置一秒钟,从一个异常基类继承是更干净的。 I would usually inherit from std::runtime_error but you could define your own if you wanted. 我通常会继承自std::runtime_error但如果需要,您可以定义自己的。 For example : 例如 :

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

A potential fix for your code: 您的代码的潜在解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM