Re: Magic cin/input buffer problem please help! :)
In message
<dd249110-f8e4-4855-ab6c-88f2b0aecef2@q11g2000yqh.googlegroups.com>,
Morgan <lianciana@gmail.com> writes
On Mar 5, 2:28?pm, red floyd <no.spam.h...@example.com> wrote:
No, that's not what I meant. ?std::terminate() is a function that gets
invoked when an uncaught exception occurs. ?Other than causing the
the program to terminate, it's behavior is (I believe) implementation
defined.
On many Unix platforms, it causes a core dump. ?On MSVC, it puts up the
message that you are seeing.
Ok!
How, exactly, should the "pick it up"? ?The footprint of at() does not
carry an exception specification, and even if it did, a function is not
required to catch or forward any exceptions in the specifications of
functions that it calls.
Well, I would have assumed (incorrectly obviously, but still) that if
your program was using an end-index in a for loop that could never be
reached by a string's at() function because the String object knows
that how large it is,
The string doesn't "know" until runtime what its size is. Granted, in
this case the compiler might be able to work it out, but mostly that's
not possible, and the compiler overhead to analyse every possible use
would be enormous. That's why at() performs a runtime test.
then that should produce an error, or at least a
warning message.
And that's exactly what it does. The at() call throws a
std::out_of_range exception. That property is precisely why you would
choose to use at() instead of []. But _you_ need to provide code that
will catch that exception, extract the message it contains, and do
something with it. For example
// warning: untested code
try
{
// do stuff...
}
catch (std::exception & e)
{
std::cerr << e.what();
}
Note that it's up to you to decide what to do with the exception. C++
doesn't automatically write it to the console for you, because there
might not _be_ a console.
You said a function isn't required to forward exceptions? For my
functions that makes sense, but surely not for functions built into
the language like mystring.at()?
You misunderstand. What Red is saying is that there's no requirement for
the function which calls at() to catch any exceptions it throws, because
it might make more sense for some function further out to handle it.
Why is that? Shouldn't (standard language functions like at()) give
useful information to the complier
Not the compiler, the runtime environment.
which could then be used to create
an informative error message? eg: the index out of bounds error I've
had on other occasions.
Add a catch clause and see for yourself...
Or am I flying on past what you were saying at a height of 20km? ;-)
<whooosh>
--
Richard Herring