来自库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());
}
}