"Subhransu Sekhar Sahoo" <su***************@nokia.com> wrote in message
news:CS********************@news2.nokia.com...
I am totally confused and i have no clue how try/catch can be implemented.
Pls explain if anyone knows how it works.
There are many ways. Here's one in a nutshell:
To implement exception infrastructure, the compiler creates
an exception record for every function (and statement block
and often also for a "virtual block" which is when a complex
declaration appears in the middle of a statement block and
therefore requires an augmentation to the cleanup routine) that
it compiles. This record contains pointers to the beginning
and end of each block's code, and a pointer to the code that
cleans up the stack when exiting the block.
To compile "catch", a new field is added to the innermost
containing exception record that indicates the entry point of
the catch routine, and a descriptor which encodes the type
of exception object being caught.
To compile "throw", a run-time library routine will be called
that [1] locates the active exception record (this can be done
because the location of "throw" will be within the beginning-of
and end-of addresses of exactly one function), [2] sees if that
record has a catch-handler for the thrown type and depending
on that, [3a] calls the catch handler, or [3b] calls the block's
stack cleanup code and restarts from [=>1] using the next
highest return address in the stack as the "location of throw".
Brief consideration of DLL's [otherwise a non-C++ topic]:
The throw-handler is necessarily on a "lower" level than the
application code. For a stand-alone application the minimum
level is run-time library. For a dynamically linked application,
it is necessary to put the handler into the same level with the
module loader and give it access (perhaps thru import/export)
to all modules' exception records.
- Risto -