Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quick question wrt std::exception

35 views
Skip to first unread message

Doug Mika

unread,
Aug 24, 2015, 1:17:27 PM8/24/15
to
Could someone explain the following line to me?

#include <exception>
struct empty_stack: std::exception
{
const char* what() const throw(); //<--What does this line do?
};

Doug Mika

unread,
Aug 24, 2015, 1:28:49 PM8/24/15
to
What I meant to ask is, shouldn't we instead have something with {} brackets and something inside? Like:

class myexception: public std::exception
{
const char* what() const throw()
{
return "My exception happened";
}
}

Bo Persson

unread,
Aug 24, 2015, 1:36:09 PM8/24/15
to
Yes, you could do that, or you could put the function in you .cpp file
and perhaps produce a more elaborate message there.


Bo Persson

Doug Mika

unread,
Aug 24, 2015, 1:50:27 PM8/24/15
to
So what does my first post do? That is, what do we have if we only have:
#include <exception>
struct empty_stack: std::exception
{
const char* what() const throw(); //No {}, so what happens without these?
};

Christopher Pisz

unread,
Aug 24, 2015, 2:07:28 PM8/24/15
to
You have a broken class that is inherited from std::exception with all
the methods that std::exception has, with what() being overriden and
inaccessable.

You'll also have good times when it is running in unicode vs multibyte.


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Christopher Pisz

unread,
Aug 24, 2015, 2:10:13 PM8/24/15
to
In your example that uses class. struct is public by default.
In that case, you just don't have any implementation and I imagine it
would produce a link error when you called myexception::what

Bo Persson

unread,
Aug 24, 2015, 2:40:45 PM8/24/15
to
You have a declaration of a member function. The function is virtual, as
it matches a virtual function from the base class.

You have to provide the full definition of the function somewhere else,
perhaps in a .cpp file.


Bo Persson

Victor Bazarov

unread,
Aug 24, 2015, 3:30:04 PM8/24/15
to
In case nobody else answers (and in case that you didn't know), the
"throw()" is the exception-specification for that member and it says
that the member function does *not* throw any exceptions. Same effect
can be achieved with keyword 'nothrow' or 'nothrow(/const-expr/)' if the
/const-expr/ evaluates to 'true' (only C++11 and later).

V
--
I do not respond to top-posted replies, please don't ask

Thomas Richter

unread,
Aug 24, 2015, 4:51:22 PM8/24/15
to
Am 24.08.2015 um 21:29 schrieb Victor Bazarov:

> In case nobody else answers (and in case that you didn't know), the
> "throw()" is the exception-specification for that member and it says
> that the member function does *not* throw any exceptions.

Actually, I would rather say that the C++ compiler ensures that the
function never throws. Actually, throw() is equivalent to

try {
.. function body ..
} catch () {
std::unexpected();
}

So there is an additional overhead included in using throw() because the
compiler has to catch exceptions within functions marked as throw().

> Same effect
> can be achieved with keyword 'nothrow' or 'nothrow(/const-expr/)' if the
> /const-expr/ evaluates to 'true' (only C++11 and later).

You probably mean "noexcept"? Actually, that is not quite equivalent to
throw(), because the former calls std::terminate() if an exception tries
to leave the function (i.e. the compiler is not required to unroll the
stack).

Greetings,
Thomas

Victor Bazarov

unread,
Aug 24, 2015, 5:53:24 PM8/24/15
to
On 8/24/2015 4:51 PM, Thomas Richter wrote:
> Am 24.08.2015 um 21:29 schrieb Victor Bazarov:
>
>> In case nobody else answers (and in case that you didn't know), the
>> "throw()" is the exception-specification for that member and it says
>> that the member function does *not* throw any exceptions.

My bad. Should have looked. The wording is "the function does *not*
allow any exceptions".

>
> Actually, I would rather say that the C++ compiler ensures that the
> function never throws. Actually, throw() is equivalent to
>
> try {
> .. function body ..
> } catch () {
> std::unexpected();
> }
>
> So there is an additional overhead included in using throw() because the
> compiler has to catch exceptions within functions marked as throw().
>
>> Same effect
>> can be achieved with keyword 'nothrow' or 'nothrow(/const-expr/)' if the
>> /const-expr/ evaluates to 'true' (only C++11 and later).
>
> You probably mean "noexcept"? Actually, that is not quite equivalent to
> throw(), because the former calls std::terminate() if an exception tries
> to leave the function (i.e. the compiler is not required to unroll the
> stack).

Yes, of course "noexcept" (was doing it from memory).

Here is what my copy of the C++11 Draft Standard says:

<<An exception-specification is
non-throwing if it is of the form throw(), noexcept, or
noexcept(constant-expression ) where the constant-expression
yields true. A function with a non-throwing exception-specification
does not allow any exceptions.>>
0 new messages