C++ 異常庫 - 建構函式



描述

這是一個建構函式異常。

宣告

以下是 std::exception::exception 的宣告。

	
exception() throw();
exception (const exception& e) throw();

C++11

	
exception() noexcept;
exception (const exception& e) noexcept;

引數

e − 這是另一個異常物件。

返回值

異常

無丟擲保證 - 沒有成員丟擲異常。

示例

以下為 std::exception::exception 的示例。

#include <iostream>       
#include <exception>      

struct ooops : std::exception {
   const char* what() const noexcept {return "Exception test!\n";}
};

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

示例輸出應如下所示:

Exception test!
std::exception 
exception.htm
廣告
© . All rights reserved.