简体   繁体   中英

Try/catch crash 255

new to the try/catch thing and need to use it for a program. For some reason though my program crashes when it catches the 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. The above 20 obj gets thrown and catches just fine. But the below 10, if its thrown will crash the program. 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.

---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!).

To fix all of your issues: 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. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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