Ian said:
But the differences between the two have little, if anything, to do with
exceptions. Try comparing the two approaches with and without exceptions
enabled. I doubt you will see any real difference.
Not really, any decent exception implementation has no or minuscule impact on
performance unless an exception is thrown. If your code does not use exceptions,
you can always turn them off at compile time.
The price, if any, of exceptions is well worth paying compared to manually
checking any operation that would otherwise throw an exception. Imagine having
to check for success each time you create a string.
Look, the code snippets above doe not check for errors at all. For code like
this, (such as s[n]printf to an automatically allocated buffer or C++ output of
few hundreds bytes), I think it is typical, and correct. This code pays price
for exceptions those.
I have been working in kernel land recently and the lack of exception support
was a real pain in an environment where resource allocation is common (and
failure more likely than in user land). Even standard idioms such as RAII become
troublesome.
Well, let me give you an example of Symbian (proudly listed by the [The
Programming Languages
Beacon](
http://www.lextrait.com/vincent/implementations.html) cited above by
Martin B. as written in C++).
Being familiar with the system quite intimately in the past, I can attest that,
true, Symbian (originally, EPOC32) was a rare example of an operating system
(very well-performing at that), almost entirely written in C++, BUT:
They did not use standard C++ exceptions in the OS. At that, they badly wanted
to use RAII (and, consequently, exceptions) and they achieved their noble goal
by creating (quite an ugly) way requiring client code cooperation via so called
"clean-up stack". That is, for the objects that you wanted to be RAII and
exception-safe, you would create an object with a (throwing, if I recall it
correctly) operator new in dynamic memory and manually put it onto so called
"cleanup stack". In case your code did not throw, you would need to clean up
your cleanup stack manually; otherwise, runtime would take care of it. You could
NOT create an exception-safe "auto" RAII object -- quite a divergence from the
standard C++ RAII implementation practice, isn't it?
(They did not use templates either but I am not sure about the reason for that
-- probably the templates were not mature enough yet in a low-common-denominator
of MSVC (that they used to build emulator binaries) and g++ (that they used to
build production binaries) -- but exceptions certainly were)
Their excuse for the "non-standard exception" mess? -- performance concerns for
using standard C++ exceptions. I tend to believe them because the guys obviously
knew everything there is to know about the design-for-performance and then some
(I am judging by the end result of their effort: at ~1998 they were selling
products built on a rock-solid hard real-time system with preemptive
multitasking, extensive support for priorities, order-of-tens-microsecond
response times for app communicating via serial and IRDA ports and perfectly
responsive nice half-VGA-size pen-interface GUI -- all powered by a poor little
7-MHz ARM CPU. I have yet to see a better-performing RTOS -- and uglier RTOS API).
-Pavel