1

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);
}
5
  • Does the console show what the error is? try putting cout<<"Enter Number between 10 and 20:"<<endl; and cin >> num; before the first try block Nov 8, 2013 at 18:57
  • The canonical way to catch multiple exceptions, is with multiple catch blocks after a single try block.
    – Chad
    Nov 8, 2013 at 19:00
  • The error is 255. Tried that but no dice
    – jrainey
    Nov 8, 2013 at 19:01
  • Yes I know but that also crashes if the number is above 20
    – jrainey
    Nov 8, 2013 at 19:03
  • Unrelated, but don't catch exceptions by value. Catch by reference.
    – n0rd
    Nov 8, 2013 at 19:11

4 Answers 4

8

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;
 };
4
  • 1
    And that is undefined behavior.
    – Chad
    Nov 8, 2013 at 18:59
  • No I didn't. It compiles but will crash if the number is above 20
    – jrainey
    Nov 8, 2013 at 19:01
  • This is almost certainly the culprit. After the call to getmsg the compiler generates code to destroy the std::string object that getmsg was supposed to return. Nov 8, 2013 at 19:06
  • dang it.. didn't notice that I called it string. Problem solved thanks
    – jrainey
    Nov 8, 2013 at 19:09
1

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

1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.