C++ 異常庫 - 概述



描述

用於獲取標識異常的字串。

宣告

以下是 std::what 的宣告。

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

C++11

virtual const char* what() const noexcept;

引數

返回值

它返回一個空終止字元序列,可用於標識異常。

異常

不丟擲保證 - 成員不丟擲異常。

示例

以下是 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;
}

示例輸出應如下所示:

Ooops! It is a identity error
exception.htm
廣告
© . All rights reserved.