I want to know how an exception object is created? and why the parameter of the handler function can be a non-constant reference?
For example:
class E{ public: const char * error; E(const char* arg):error(arg){ cout << "Constructor of E(): ";} E(const E& m){ cout << "Copy constructor E(E& m): " ; error=m.error; } }; int main(){ try{ throw E("Out of memory"); } catch(E& e){cout << e.error;} }
Exit: Constructor E (): from memory
therefore, I throw E("out of memory") and E("out of memory") is just a temporary object, and the object was not created except for E("out of memory") because the copy constructor was not called. so even if this E("out of memory") is just a temporary object, I have a handler that accepts a non-constant reference.
Can you explain to me why this is possible?
c ++ object exception lifetime
Alexdan
source share