1

In C++ concurrency in Action page 45 I have this code

#include <exception>
#include <memory>

struct empty_stack: std::exception
{
   const char* what() const throw();  //<--- what does this mean?
}

Can anyone tell me what does this mean?

this empty_stack exception is being thrown by another function if a stack is empty, such as

if(data.empty()) throw empty_stack();

but what does the line inside mean?

EDIT:

Someone posted (but was removed! wonder why) this link Thanks!

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 3
    [Throws nothing = doesn't throw = `noexcept`](https://en.cppreference.com/w/cpp/language/noexcept_spec). – Evg May 10 '20 at 09:05
  • It declares the method [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) that returns the exception description. Do you mean the entire line or some particular part of it? – bereal May 10 '20 at 09:08
  • The entire line. So `what` is just a name for a method? (ergo could be anything?) – KansaiRobot May 10 '20 at 09:10
  • 2
    @KansaiRobot `std::exception` provides a virtual method `what`, so no, you must use this specific name to override it. – HolyBlackCat May 10 '20 at 09:13
  • @HolyBlackCat Thanks! Someone posted a link (and deleted) about this. – KansaiRobot May 10 '20 at 09:22
  • 1
    @Evg -- `throw()` is not quite the same as `noexcept`. See [here](https://stackoverflow.com/questions/37433371/is-there-any-difference-between-noexcept-and-empty-throw-specification-for-an-la). The difference is that if the function throws an exception, if it's marked `throw()` you'll get a call to `std::unexpected()`; if it's marked `noexcept` you'll get a call to `std::terminate()`. `std::unexpected()` by default calls `std::terminate()`, but it's replaceable, so you aren't bound to terminate. – Pete Becker May 10 '20 at 12:50
  • @PeteBecker, thanks for the link! cppreference says that since C++17 `throw() = noexcept`. Before C++17, stack unwinding is guaranteed. – Evg May 10 '20 at 12:56
  • @Evg -- hmm, it's more subtle than I thought. I haven't been paying attention lately... – Pete Becker May 10 '20 at 13:01

1 Answers1

3
 const char* what() const throw();
 |-> return type
             |-> name of the method
                 |-> parameter
                    |-> it is a const method
                          |-> exception specification

It is a const method called what that takes no parameters, returns a const char * and is specified to not throw an exception.

Note the dynamic exception specifications are deprecated in C++11 and removed in C++17/20. For what it does I quote cppreference:

If the function throws an exception of the type not listed in its exception specification, the function std::unexpected is called. The default function calls std::terminate, but it may be replaced by a user-provided function (via std::set_unexpected) which may call std::terminate or throw an exception. If the exception thrown from std::unexpected is accepted by the exception specification, stack unwinding continues as usual. If it isn't, but std::bad_exception is allowed by the exception specification, std::bad_exception is thrown. Otherwise, std::terminate is called.

Evg
  • 25,259
  • 5
  • 41
  • 83
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185