c++中try catch的用法-1

来自库muduo:

打印栈信息参考:http://blog.csdn.net/ygm_linux/article/details/25888253

Exception.h

#ifndef _Exception_H_
#define _Exception_H_
#include <iostream>
#include <string>
using namespace std;

/*
那么throw()究竟有何意义呢?
其实,与其称其为"空异常描述符",
还不如称其为"中断描述符".
简单的说,如果throw()修饰的函数中发生任何异常,
即便函数外部有捕获操作,函数也不会抛出任何异常,
那么程序将直接终止!!!

简单的说,对使用throw()修饰的函数进行try{}catch(...){}
是没有任何意义的!顾,在使用throw()修饰函数的时候,
请务必确认,函数在任何情况下都不会发生异常.
*/
class Exception : public std::exception
{
 public:
 /*防止隐式转换的*/
  explicit Exception(const char* what);
  explicit Exception(const string& what);
  virtual ~Exception() throw();
  virtual const char* what() const throw();
  const char* stackTrace() const throw();

 private:
  void fillStackTrace();

  string message_;
  string stack_;
};

#endif

Exception.cpp

#include <execinfo.h>
#include <stdlib.h>
#include "Exception.h"

/*保存用户信息于message_;
将堆栈信息存于stack_;*/
Exception::Exception(const char* msg)
  : message_(msg)
{
  fillStackTrace();
}

/*保存用户信息于message_;
将堆栈信息存于stack_;*/
Exception::Exception(const string& msg)
  : message_(msg)
{
  fillStackTrace();
}

Exception::~Exception() throw ()
{
}

/*返回用户信息*/
const char* Exception::what() const throw()
{
  return message_.c_str();
}

/*返回堆栈信息*/
const char* Exception::stackTrace() const throw()
{
  return stack_.c_str();
}

void Exception::fillStackTrace()
{
  const int len = 200;
  void* buffer[len];
  int nptrs = ::backtrace(buffer, len);
  char** strings = ::backtrace_symbols(buffer, nptrs);
  if (strings)
  {
    for (int i = 0; i < nptrs; ++i)
    {
      // TODO demangle funcion name with abi::__cxa_demangle
      stack_.append(strings[i]);
      stack_.push_back('\n');
    }
    free(strings);
  }
}

test.cpp

#include <stdio.h>
#include "Exception.h"


int main()
{
  try
  {
    throw Exception("oops");
  }
  catch (const Exception& ex)
  {
    printf("reason: %s\n", ex.what());
    printf("stack trace: %s\n", ex.stackTrace());
  }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值