Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

exception::~exception

公众成员函数  <exception>

C++98
virtual ~exception throw();

析构异常
销毁异常对象。

作为虚函数,派生类可以重新定义其行为。

☲  参数


none

☣  示例



// exception virtual destructor
#include <iostream>       // std::cout
#include <exception>      // std::exception
#include <cstring>        // std::strlen, std::strcpy

// text_exception uses a dynamically-allocated internal c-string for what():
class text_exception : public std::exception {
  char* text_;
public:
  text_exception(const char* text) {
    text_ = new char[std::strlen(text)+1]; std::strcpy (text_,text);
  }
  text_exception(const text_exception& e) {
    text_ = new char[std::strlen(e.text_)+1]; std::strcpy (text_,e.text_);
  }
  ~text_exception() throw() {
    delete[] text_;
  }
  const char* what() const noexcept {return text_;}
};

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

输出:
custom text

☂ 异常安全性



无抛出保证:该成员函数从不抛出异常。
这也适用于c++标准库中的所有派生类。

🍄  另请参阅



exception::exception 构造异常(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图