Arjen Markus wrote: > On 2009-09-25 01:43, Alan W. Irwin wrote: > >> That transfer of control part, is the part I didn't understand. Although I >> have very little C++ experience you got me interested enough to look at a >> C++ tutorial on exceptions >> (http://www.deitel.com/articles/cplusplus_tutorials/20060325/), but that >> didn't really answer my question either. If you throw an exception many >> routine calls deep in the code does control immediately jump to the catch >> block after the try block in the calling code or is all the intervening >> function processing and returning involved (which would then necessarily >> call exit)? >> >> I guess from what you have said, the answer to that rhetorical question must >> be "immediate jump", but there must be a very special mechanism to make the >> code block code location available to _all_ code (no matter how many routine >> calls are used to get to that code) which _might_ throw an exception. >> >> I was well aware Python had exception handling, but I never thought too much >> until now about the machine code practicalities involved in the code jump >> involved (and skipping all intervening code) in transferring control in a >> different way than the normal function return mechanism when an exception >> has been thrown. >> > > If I understand these matters correctly, then the stack is unwound until > a function is encountered that does want to take care of the exception. > (At the very least this is what happens in Java. I am not too sure about > C++, but I think it works there in the same way.) Once an exception is > thrown, the clean-up code for a function must come into action to get > rid of the local variables that can no longer exist (this includes > finalising objects).
This indeed happens with exceptions in C++. It unwinds the call stack and cleans up the local variables (including objects) until there's a handler for the exception. So regardless whether a function exits due to returning normally or due to unwinding the stack during an exception, the local variables are cleaned up normally. This feature is used in the common C++ idom Resource Acquisition Is Initialization (RAII) [1]. [1] http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization Regards, Mark de Wever ------------------------------------------------------------------------------ Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________ Plplot-devel mailing list Plplot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/plplot-devel