I just read about exceptions over on learncpp.com, and while I think I understand most of it, I have one lingering question that I can't find a clear answer to: how are try/catch blocks located "higher" in the program than the throw statement able to access thrown objects, and when are these objects destroyed? When stack unwinding occurs, other (stack-allocated) objects in the current scope have their destructors called during the unwinding process, but the exception object obviously persists. Does that mean it exists in the heap, despite its instantiation looking like that of an anonymous stack object (throw ExceptionClass();)? Even if that's right, though, functions higher in the chain need some way to know the object's address and type so that they can handle it or pass it on, and I'm having a hard time wrapping my head around exactly how that would happen given my knowledge of ways in which objects/references/pointers can normally be passed around in C++. Maybe there is a special memory location set aside especially for references to thrown objects?
Also, are thrown objects destroyed at the end of the first catch block that can receive them, or some other time?
p.s. sorry if any of the above is inaccurate or just plain wrong, I'm pretty much just starting out!
As long as my programming professors know what they are talking about, thrown exception objects are stack-allocated and destroyed when they go out of scope, basically after execution leaves the try-catch block, just like an object of any other type would be destroyed after it went outside of scope.
If you want to retain the exception object, you would need to pass it into a container before it was destroyed (like a vector). Exception objects have their own destructors. When you throw an exception, it is passed by reference to the block of code written to catch the exception (if no code exists to catch it, it is thrown to the next highest level program, which will usually be your operating system, so you program will halt).
To make it simple, the exception object exists in its own storage, not on any stack. It is destroyed when the matched catch clause ends by means other than rethrowing or ( in C++11) the last exception_ptr gets destroyed.
Whoops. I didn't see your edit at 8:52.
To be honest, I do not know about exceptions extensively, but I understand them on a basic level. I hope this helps. If I am incorrect, please correct me!
When you throw an exception, the program tries to look for a catch to handle the exception, even if that catch is in a function higher up in the hierarchy. If you are in a function (other than main) and an exception is thrown, the function will stop and control will return to main and it will look at the catch statement right after the try block to see if it can handle the thrown exception. If an exception is thrown in main, then the program will exit the try block and look at the catch block to see if it can be handled. If it can, then it will do so, and the exception object will go out of scope after your program execution goes past the catch block. After that, your program will continue to execute as normal.
Remember that if you have a bunch of code in your try block, that code will not be executed if an exception is thrown before those lines of code are reached.
The destruction of an exception object works a bit differently when compared to other kinds of stack-allocated objects.
It does this so that it can keep throwing the exception to functions higher up until it is caught or your program is shut down.