KansaiRobot
KansaiRobot

Reputation: 9982

what does what() const throw mean?

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!

Upvotes: 1

Views: 742

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122830

 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.

Upvotes: 3

Related Questions