Re: The D Programming Language
Gerhard Menzl skrev:
peter koch larsen wrote:
Gerhard Menzl skrev:
peter koch larsen wrote:
Wrong again. C++ throw() guarantees that the function will not
throw anything, An empty Java throw specification on the contrary
guarantees nothing of that kind.
Surely you mean to say that C++ throw() guarantees that
std::unexpected() will be called in case that the function throws in
spite of having promised not to.
No. I meant what I wrote. That std::unexpected gets called is a detail
that does not invalidate my point, so I'm unsure what point you're
trying to make. Are you suggesting that std::unexpected might itself
throw an exception that will get by the throw() specification? If that
is the case, I believe you're wrong. That behaviour would rather form
an infinite loop.
You stated that an empty exception specification guarantees the function
will not throw anything.
Yup. In the context I believed it to be quite evident that the
indication was that no exception would escape the function. In
retrospect this is unclear.
But what it actually guarantees is that no
exception exits the function. Your choice of terms was at least
misleading: someone unfamiliar with the C++ exception mechanism could
easily interpret it as describing a compile-time check, which is
precisely what C++ does not offer.
To avoid this confusion, especially
when comparing C++ with Java, which does have static checks, I think it
is important to distinguish between "cannot throw" and "will abort if it
throws".
Actually - to increase confusion - it should be clear that the check in
Java is only of exceptions that derive from the class Exception.
Exceptions deriving from Throwable and RuntimeError (if I remember
correctly) are not checked at all.
Also, I am unsure of the quality guaranteed by the language. Example:
(C++ used as example to avoid excessive number of classes):
bool is_small_prime(int i) { return i == 2 or i == 3 or i == 5; }
void f(int i) { if (is_small_prime(i) throw Exception("Ups"); }
void never_throws(int i) throw()
{
if (!is_small_prime(i))
f(i);
}
Will the compiler complain about the signature of never_throws? My
guess is that it will and thus you must write a swallowing catch which
in my opinion is bad for releability.
/Peter
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]