M
Marc
Why do the operations of scoped_ptr use undefined behavior instead of
throwing?
throwing?
Why do the operations of scoped_ptr use undefined behavior instead of
throwing?
scoped_ptr is designed to emulate a pointer; raw pointers have
undefined behavior, so so does scoped_ptr.
Throwing an exception is, of course, the wrong thing.
On 15/12/2010 15:01, James Kanze wrote:
If you discover a programming error you should fix the programming
error.
Out of bounds std::vector::at() throws an exception; it doesn't
abort. Out of bounds std::vector::at() is a programming error.
One wonders what std::logic_error is for, then: "The class logic_error
defines the type of objects thrown as exceptions to report errors
presumably detectable before the program executes, such as violations
of logical preconditions or class invariants."
At the very least, you can catch std::logic_error at the top level and
attempt emergency shutdown. If your code is operating an X-ray
machine, the patient will probably be grateful that the power gets cut.
Where as crashing causes the emergency handling (backup, etc.)
to come into play, which does ensure that the X-rays are turned
off.
One wonders what std::logic_error is for, then: "The class logic_error
defines the type of objects thrown as exceptions to report errors
presumably detectable before the program executes, such as violations
of logical preconditions or class invariants."
At the very least, you can catch std::logic_error at the top level and
attempt emergency shutdown. If your code is operating an X-ray
machine, the patient will probably be grateful that the power gets cut.
James Kanze said:If you're controlling an X-ray machine or anything like that,
you must abort at the slightest hint of a problem, executing the
least code possible.
Who says that unrolling the stack will cut
the power---the code has done something wrong, the internal
state is incoherent, and executing the destructors could
possibly increase the dose by a couple of orders of magnitude.
Where as crashing causes the emergency handling (backup, etc.)
to come into play, which does ensure that the X-rays are turned
off.
Ah, but what if you're the poor sap who's writing the emergency
handling?
You can't keep aborting and hope there's another layer of backup...
Leigh Johnston said:I would actually be rather worried if I knew the control software of an
X-ray machine I was lying under was written in C++. Why? C++
programmers.![]()
Leigh Johnston said:I throw std::logic_error. I do not catch std::logic_error. The program
aborts.
It is no better and no worse; it does potentially provide more
information: one could potentially catch the logic_error at main() (or
equivalent), print out the associated error string and then re-throw for
example; I don't do this however; I just rely and the default behaviour
of calling terminate/abort.
Leigh Johnston said:It is no better and no worse; it does potentially provide more
information: one could potentially catch the logic_error at main() (or
equivalent), print out the associated error string and then re-throw for
example; I don't do this however; I just rely and the default behaviour of
calling terminate/abort.
James said:scoped_ptr is designed to emulate a pointer; raw pointers have
undefined behavior, so so does scoped_ptr. (One can argue
whether this is a good thing or not, but nothing in undefined
behavior prevents an implementation from doing the right thing,
e.g. inserting an assert.)
Throwing an exception is, of course, the wrong thing. If you
discover a programming error, you should abort (in most
applications, anyway---there are exceptions).
James said:One does wonder, yes.
If you're controlling an X-ray machine or anything like that,
you must abort at the slightest hint of a problem, executing the
least code possible. Who says that unrolling the stack will cut
the power---the code has done something wrong, the internal
state is incoherent, and executing the destructors could
possibly increase the dose by a couple of orders of magnitude.
Where as crashing causes the emergency handling (backup, etc.)
to come into play, which does ensure that the X-rays are turned
off.
Leigh Johnston said:std::vector::at throws std:ut_of_range which is-a std::logic_error.
Marc said:"most applications"? Can you qualify that? When you say "most" without
qualifying it, then the referrent set of applications is ALL applications,
from the one in that nifty Star Trek watch to the GUI on your desktop (not
that I am suggesting that those 2 examples really define endpoints).
Leigh said:It is not compulsory for an implementation to unwind the stack when an
unhandled exception is thrown however I agree that possible stack
unwinding may be unwise in safety critical software.
Leigh Johnston said:std::string::append can also throw std:ut_of_range which is-a
std::logic_error; I am sure there are plenty of other examples of the
standard library throwing exceptions which are derived from
std::logic_error.
If it is fine for the C++ standard library to throw std::logic_error then
it is fine for code which uses the C++ standard library to throw
std::logic_error; horse already bolted etc.
Leigh said:You want to run as little code within the context of the errant
process as possible when dealing with errors in safety critical
systems; abort can therefore be seen as the more appropriate action
rather than stack unwinding for such systems. Different applications
require different degrees of defensiveness; write code (and coding
standards) accordingly.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.