Board logo

标题: valgrind and throw [打印本页]

作者: cnangel    时间: 2012-7-25 10:54     标题: valgrind and throw

  1. #include <stdexcept>
  2. #include <iostream>
  3. #include <vector>

  4. class ValgrindRuntimeError : public std::exception {

  5.   public:
  6.   /** Takes a character string describing the error.  */
  7.   ValgrindRuntimeError(char s[]) {
  8.     _s=new char[strlen(s)];
  9.     strcpy(_s, s);
  10.   }

  11.   virtual ~ValgrindRuntimeError() throw() {
  12.     delete[] _s;
  13.   }

  14.   /** Returns a C-style character string describing the general cause of
  15.    *  the current error (the same string passed to the ctor).  */
  16.   virtual const char* what() const throw() {
  17.     return _s;
  18.   }
  19.   private:
  20.     char *_s;
  21. };

  22. int main() {
  23.   ValgrindRuntimeError a(std::string("xxxx").c_str());
  24.   throw a;
  25. }
复制代码





欢迎光临 星星博客 (http://commerce.huhoo.net/) Powered by Discuz! 7.0.0