Reputation: 3015
What's the correct way to define a user-defined run-time exception in c++?
I want to define a simple exception and be able to store a string message value in it?
I can't find any examples on this.
Upvotes: 2
Views: 1285
Reputation: 63190
class my_exception : public std::exception
{
public:
my_exception(const std::string& msg) : msg_(msg) {}
const char* what(); // override what to return msg_;
private:
std::string msg_;
};
//some other code..
throw my_exception("Error");
This is how you'd create a new run-time exception. It's just a class
Upvotes: 1
Reputation: 92211
You can find some predefined exception types in the header <stdexcept>
.
Either use one of those or derive your class from it. It has all the machinery already implemented.
Upvotes: 4