alex555
alex555

Reputation: 1786

How to throw std::bad_alloc with a custom message in C++?

Since VS2008 the class bad_alloc does not provide a constructor with string parameter. Is there any possibility to create a custom message without overwriting the class like this?

// this is only pseudo-code
class custom_exception : bad_alloc {
public:
    string Message;
    custom_exception(string m) {Message = m;}
}

Upvotes: 4

Views: 2477

Answers (1)

ecatmur
ecatmur

Reputation: 157414

bad_alloc needs to be constructible without allocating memory, which is why its constructor does not take a message parameter. You can of course subclass it and override what.

Upvotes: 7

Related Questions