Reputation: 1
Does anyone have a copy of the source code for exception class?
I would like to study it's implementation.
Thanks
edit: I am looking for std::exception
I am interested in it because I would like to know how the constructor takes a char* and initializes it's member variable, and how the copy constructor, assignment operator is done in this class.
Upvotes: 0
Views: 1206
Reputation: 355207
There is no base "exception class" in C++: you can throw just about anything, even an int
(throw 42;
is quite valid).
If you're talking about the std::exception
class, there is very little in it: none of its member functions actually need to do anything (what()
just has to return a pointer to some C string). The following would be a completely correct implementation:
struct exception {
exception() throw() { }
exception(const exception&) throw() { }
exception& operator=(const exception&) throw() { }
virtual ~exception() throw() { }
virtual const char* what() const throw() { return "o noez! an exception!"; }
};
The exception classes in the Standard Library that allow you to specify your own string (for example, std::runtime_error
) use std::string
. They shouldn't have to do any manual memory management because they can use std::string
. (Technically, an implementation doesn't have to use std::string
internally, but it does have to take one in its constructor.)
Upvotes: 1