C++ Exception Library - what


Advertisements

Description

It is used to get string identifying exception.

Declaration

Following is the declaration for std::what.

virtual const char* what() const throw();

C++11

virtual const char* what() const noexcept;

Parameters

none

Return Value

It returns a null terminated character sequence that may be used to identify the exception.

Exceptions

No-throw guarantee − no members throw exceptions.

Example

In below example for std::what.

#include <iostream>       
#include <exception>      

struct ooops : std::exception {
   const char* what() const noexcept {return "Ooops! It is a identity error\n";}
};

int main () {
   try {
      throw ooops();
   } catch (std::exception& ex) {
      std::cout << ex.what();
   }
   return 0;
}

The sample output should be like this −

Ooops! It is a identity error
exception.htm
Advertisements