I want to know how the exception object is created ? and why the handler function parameter can be a non-const 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;}
}
Output:
Constructor of E(): Out of memory
so I have throw E("out of memory")
and E("out of memory")
is just a temporary object and no object has been created except E("out of memory")
because no copy constructor has been invoked. so even though this E("out of memory")
is just a temporary object, I have a handler that take a non-const reference.
Can you explain to me why this is possible?