Which will have additional overhead to allocate, construct,
destroy and deallocate the application class derived from std::exception, nicht wahr?
Only in the exceptional circumstances where one is thrown. As I said,
the state has to be returned somewhere.
I think you would find that "small nested functions" is exactly what is
used, and in this case, the object passed doesn't necessary _return_ the state
passed so much as provide a channel for returning the state to the final
receipient.
Now in the core processor simulation code sigsetjmp/siglongjmp are used to reset
the simulated processor to the start of the instruction processing loop when
an exception occurs executing an instruction (an address error, invalid instruction,
etc). These could possibly be converted to exceptions, but I'm quite sure that
the performance would be worse and the RAS characteristics of the application
wouldn't change appreciably.
By using exceptions, you are able to utilise the full power of the
language, such as RAII which out of band jumps preclude.
One point I have been trying to make is bolting exceptions onto an
existing design is seldom a good idea. Neither is having to take them
out, which was a situation I once found myself in! Error handling is a
fundamental part of a system's design and mixing paradigms invariably
ends in tears.
I remember reading Tom Cargill's article on exceptions back when they were
introduced to the language - I think many of his points still apply - exceptions
are not a universal panacea and careful analysis of all codepaths need be done
to prevent memory leaks or access to deallocated data when exceptions are being
thrown.
This is true and the language provides us with the tools to achieve
this. Tools which cannot be used with out of band jumps!
The same caveats, of course, apply to setjmp/longjmp as well, but I
don't need to allocate an exception object (just set one of 10 bits in a member
field of the current 'this' when the longjmp is issued; the jmp_buf also a member
of the current object).
The cost of constructing an exception object are far outweighed by the
cost of throwing it *in the exceptional case*.
I haven't seen this in practice, but then my sole exposure to C++ code
has been in the Operating Systems/Microkernel/Hypervisor area none of which
used exceptions (most of which predated exceptions
, aside from one
application at a major internet certification authority, which also predated
exceptions.
That's where our experiences diverge, I've been comparing the
performance of exceptions on various platforms for many years and I've
found exceptions are generally faster (always on the Unix and Linux
platforms I use). From my perspective, the exceptions vs error returns
argument was settled long ago.
Let me be clear, I never actually took an exception in the test I described above;
the sole difference was changing the
buf = malloc(XXX); if (buf == NULL) { YYY}
test to
try {buf = new uint8[XXX];} catch (std::exception& e) { YYY }
(and of course, the corresponding 'free()'<-> 'delete []' changes).
You have introduced two variables, exceptions and new. Try eliminating
both.