0

I have made my own exception class which derives from runtime_error and is getting an int in the c'tor.

I would like to make a base class for this exception, in order to use polymorphism, so I could catch only the base class and basically I would be catching the derived class, and then call .what() method from it.

So, this is the base class: (ofc in another cpp file I got baseException::~baseException(){})

class baseException
{
    virtual ~baseException()=0 {}
    virtual const char* what()=0;
};

And this is the derived class:

class myException: public runtime_error, public baseException
{
public:
    myException(int): runtime_error("Error occured") {}
    const char* what() {return runtime_error::what();}
};

But when in the main I write:

catch(baseException* x)
{
cout<<x->what();
}

it just skips it and does not enter the block, even though myException inherits from baseException. Any suggests?

7
  • 1
    What do you mean you can not? Jun 18, 2012 at 13:00
  • 1
    Why not let baseException inherit from std::runtime_error and myException only from baseException? Jun 18, 2012 at 13:03
  • Also, how do you use throw and catch? Please add that code to the question. Jun 18, 2012 at 13:03
  • @GazTheDestroyer: that's defined by std::runtime_error (assuming that's what the OP means by runtime_error). Jun 18, 2012 at 13:04
  • edited so it would be clearer thanks guys!
    – Jjang
    Jun 18, 2012 at 13:08

4 Answers 4

2

You should catch exceptions by reference (or const reference), not by pointer.

5
  • why? so how can I do polymorphysem if i catch by reference?
    – Jjang
    Jun 18, 2012 at 13:09
  • polymorphism is compatible with references, test it out Jun 18, 2012 at 13:14
  • @LuchianGrigore - why? It addresses exactly the original question.
    – ecatmur
    Jun 18, 2012 at 13:36
  • So catching by reference fixes the problem? Jun 18, 2012 at 13:47
  • At least one of the more obvious problems, anyway.
    – ecatmur
    Jun 18, 2012 at 13:49
2

Your baseException doesn't have the what method, you should probably just derive baseException from runtime_error.

class baseException : public runtime_error
{
public:
    baseException(const std::string& what) : runtime_error(what) {}
};

and then

class myException: public baseException
{
public:
    myException(int): baseException("Error occured") {}
};

Although I prefer the following idiom:

class myException: public baseException
{
public:
    myException(int x): baseException(getWhatMessage(x)) {}

private:
    static std::string getWhatMessage(int x) { /*generate the message*/ }
};

On the catch part. If you throw using throw myException(5), then you should catch like this

catch(baseException& x)
{
    cout<<x.what();
}
2
  • but each one of the derived exceptions class has a different message and is getting different objects, some get ints some strings etc
    – Jjang
    Jun 18, 2012 at 13:11
  • so? add another myException2 taking double as the argument, what's the problem? Jun 18, 2012 at 13:12
1

Your catching a reference to a baseException object; therefor you just know the methods of that class. baseException does not have a member called what() though. This causes the error. Make baseException derive from runtime_error or catch a myException directly.

Edit:

This snippet shows that theres absolutely no reason why pointers shouldnt work together with exceptions:

#include <iostream>
#include <string>

class A {
public:
    virtual int test() = 0;
};

class B : public A {
public:
    virtual int test() {
        return 42;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    try {
        throw new std::string("foo");
    } catch (std::string* ecx){
        std::cout << *ecx << std::endl;
    }

    try {
        throw new B();
    } catch (A* ecx) {
        std::cout << ecx->test() << std::endl;
    }
}

Output:

foo

42

7
  • baseException inherits from runtime_error not an option, since each one of my derived custom exceptions sends a different message to runtime_error c'tor.
    – Jjang
    Jun 18, 2012 at 13:08
  • @Jjang: then pass that message in through the baseException constructor to runtime_error.
    – rubenvb
    Jun 18, 2012 at 13:08
  • my answer contains an example of doing this Jun 18, 2012 at 13:09
  • -1 because this answer is wrong. If it didn't have a method it wouldn't even compile. The problem is catching a pointer when it should be a reference.
    – Jarryd
    Jun 18, 2012 at 14:05
  • @Jarryd At the time i wrote this answere the OP hasnt made his question more concrete. Besides that a quick test shows that its completely legal to throw pointers. Although your code will become exception unsafe. References are a better way due to the RAII pattern. Jun 18, 2012 at 14:29
1

UPDATE: this answer was based on the original version of the question. It now seems that the problem isn't calling what() (as you've worked around that by redeclaring it in your base class). The problem is simply that you're trying to catch a pointer and (I guess) throwing a value; the solution is to catch by reference:

catch (myException const & ex) {
    std::cerr << ex.what() << std::endl;
}

(assuming you fix your declaration of what() to be const; if for some reason you really need it to be non-const, then remove const from the catch line).

ORIGINAL ANSWER describing how to call what() if it isn't declared in baseException:

I want to catch baseException*

You'd be better off catching baseException const &; there's no sensible way to throw a pointer.

and call their .what() methods

If you want to call what() , then you might be better off catching std::exception const & instead; unless you also want some functionality from your base class. In that case, perhaps your base class should inherit from std::runtime_error; or perhaps it should inherit from std::exception, in which case your myException type would need to use virtual inheritance.

If you really want to access what() from your classes as they stand, then you'll need to cross-cast to std::exception:

catch (myException const & ex) {
    std::cerr << dynamic_cast<std::exception const &>(ex).what() << '\n';
}
2
  • why go after the begginer into this multiple inheritance madness and not just point the right way to do it? Jun 18, 2012 at 13:17
  • @unkulunkulu: Because I don't know exactly what the OP is trying to achieve, and so I don't know what the "right" way is. One of the options I gave is to use single inheritance, but I don't know whether that will meet his needs so I also described other options. Jun 18, 2012 at 13:19

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.