This error is unique to MS compilers I think (and I think it's
optional there). With g++ under Linux (and all other Unix
compilers, I think), and access violation results in a core
dump, which is far preferrable for most applications.
The reason C++ doesn't have backtraces in exceptions is that
that's not what exceptions are for. Exceptions are designed to
be caught, so any backtrace would be ignored. If you have a
programming error, you want to stop immediately (in most
applications, anyway). Violently, and with a backtrace. In the
core file, or its equivalent---your client can't do anything
with a backtrace, but he can send you a core dump, so that you
can do something. (And of course, if the core dump isn't in
your test cases, you're not testing sufficiently.)
I had the following scenario: Software for quality assurance, it
controls a measurement machine with cameras and stage. Measurements take
up to 8 hours, so the user should be able to abort measurements if he
sees that the measured object is not good enough.
I implemented the aborting of measurements by means of an exception, so
that the stack is neatly unwound and the cameras stop acquisition and
the stages will be powered off. With exceptions this is rather
comfortable, since I don't have to clutter my code with
Picture* picture = 0;
if (acquirePicture (&picture) == ERROR_CODE_SHOULD_STOP)
return ERROR_CODE_SHOULD_STOP;
but simply write
Picture* picture = acquirePicture ();
Now I have the problem that I am bound to the Windows platform
(drivers), so when I do something wrong and get an Access Violation,
I'll get an BAD_ACCESS exception. This is actually a good thing because
my application does not shut down without a pip, but the stack is
unwound and the measurement machine returns to a defined state (stages
stopped, camera in idle mode). But unfortunately I have no more
information about where the Access Violation occurred since C++
exceptions are designed to be used in a different way. Well, this works
well for my StopException since I don't care which particalur action has
been performed when the user aborted the measurement, but in case
something went wrong, I'd like to see what happened.
I ended up setting up my own stack that can be examined later on, but
this is rather clumsy and requires all my actions to push themselves on
this stack (not too bad) and pop when they have finished. The later is
quite cumbersome since every code path where the action stops without an
exception has to ensure that the action pops itself from the stack, or
else my stack gets messed up.
A perfect language would generate an exception with a stacktrace upon an
Access Violation, which is much more preferable to a Core Dump. For
those who actually want to have the core dump, the language should
generate such a dump when the exception is not caught (AFAIK, Win32 does
not offer a setting that generates dumps upon Access Violations).
Thanks in advance,
Stuart
PS: @Jorgen: Come on, you may not know every little bit of the language
(who does), but certainly you know a LOT more than the average C++
programmer. I tend to read most of your posts because changes are good
that I can learn something new.
@James: The same goes for you.