first model:
class MyException
{
string message;
public:
MyException( string msg ): message( msg ) {};
void printmsg() { cout<< message <<endl<<endl; }
};
void willthrow()
{
throw MyException("mk");
}
int main(int argc, char * argv[])
{
try
{
willthrow();
}
catch( MyException &ex)
{
ex.printmsg();
}
}
second model:
class MyException
{
string message;
public:
MyException( string msg ): message( msg ) {};
void printmsg() { cout<< message <<endl<<endl; }
};
void willthrow()
{
throw new MyException("mk");
}
int main(int argc, char * argv[])
{
try
{
willthrow();
}
catch( MyException *ex)
{
ex.printmsg();
delete ex;
}
}
it is clearly that in the second implementation the MyException object is allocated on heap, but how about the first one? What's the mecanism behind it? It would look like it's placed on the stack and then cought by reference, but how can it be placed on the stack if that is what exceptions do, unwind the stack till an apropriate catch clause is found. I mean, the stack pointer drops and when u catch the exception the reference points to an exception object above the current stack pointer... I don't know maybe i'm wrong. Can someone please explain how things actually work?