public virtual member function
<exception>
virtual ~exception throw();
Destroy exception
Destroys the exception object.
As a virtual function, derived classes may redefine its behavior.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// exception virtual destructor
#include <iostream> // std::cout
#include <exception> // std::exception
#include <cstring> // std::strlen, std::strcpy
// text_exception uses a dynamically-allocated internal c-string for what():
class text_exception : public std::exception {
char* text_;
public:
text_exception(const char* text) {
text_ = new char[std::strlen(text)+1]; std::strcpy (text_,text);
}
text_exception(const text_exception& e) {
text_ = new char[std::strlen(e.text_)+1]; std::strcpy (text_,e.text_);
}
~text_exception() throw() {
delete[] text_;
}
const char* what() const noexcept {return text_;}
};
int main () {
try {
throw text_exception("custom text");
} catch (std::exception& ex) {
std::cout << ex.what();
}
return 0;
}
|
Possible output:
Exception safety
No-throw guarantee: this member function never throws exceptions.
This also applies to all derived classes within the C++ standard library.