Re: In which case a function / method is guarantee not to throw an
exception ?
On Apr 9, 7:49 am, tonydee <tony_in_da...@yahoo.co.uk> wrote:
On Apr 8, 9:59 pm, Roberto Giaffy <gia...@gmail.com> wrote:
Thank again for the answer;
so the list for determining what/when something could throw an
exception
could start with:
1 - "throw()" statement;
2 - dynamic_cast on a reference type
3 - standard library functions (documented as throwing)
James Kanze said elsethread:
At the library level, in practice, I would assume that any
function which doesn't specifically document that it cannot
throw might.
I generally assume what I tend to think of as "C functions"
will not throw - by which I mean things like strcmp, isalnum
etc. - although I guess an implementation would be free to
have some C++-specific checks/ throws #ifdef-ed in for
precondition violations....
Good point. I could, I suppose, beg off by saying that the fact
that they can be called from C is implicit documentation that
they cannot throw (with the exception of those which use a
callback, like qsort and bsearch). Although technically: all
that is required of the C library functions in C++ is that they
have the correct behavior. There's nothing to prevent a C++
compiler from coming with its own implementations, 100% C++.
(As far as I know, the only restriction is that the
implementation of malloc cannot use std::operator new. Still, I
tend to assume that other functions in the C library won't use
C++ functionality either: my implementation of a debugging
operator new uses memcpy and memcmp, for example, and if one of
those calls operator new, I'll end up with an endless
recursion.)
[...]
Note: the code provided in the 2st post was just an example,
I would like to understand when a method/function can be
qualified with throw() - a no- throwing function;
It really is just a matter of knowing that any
potentially-throwing code beneath it already has catch()
blocks. Still, the only good reason I've seen for throw() was
on an old Sun C++ where some functions wouldn't inline without
it.
An empty expression specification (throw()) is an important
guarantee. It's impossible to write exception safe code without
it.
--
James Kanze