菜鸟笔记
提升您的技术认知

c 的异常处理和堆栈信息-ag真人游戏

c 标准异常

#include 
// std::exception

堆栈信息

获取堆栈信息,主要是使用execinfo.h中定义的几个函数.

在抛出异常之后,通过execinfo.h中定义的函数,获取堆栈信息,有助于定位问题.

/* ag真人试玩娱乐 copyright (c) 1998-2016 free software foundation, inc.
   this file is part of the gnu c library.
   the gnu c library is free software; you can redistribute it and/or
   modify it under the terms of the gnu lesser general public
   license as published by the free software foundation; either
   version 2.1 of the license, or (at your option) any later version.
   the gnu c library is distributed in the hope that it will be useful,
   but without any warranty; without even the implied warranty of
   merchantability or fitness for a particular purpose.  see the gnu
   lesser general public license for more details.
   you should have received a copy of the gnu lesser general public
   license along with the gnu c library; if not, see
   .  */
#ifndef _execinfo_h
#define _execinfo_h 1
#include 
__begin_decls
/* store up to size return address of the current program state in
   array and return the exact number of values stored.  */
extern int backtrace (void **__array, int __size) __nonnull ((1));
/* return names of functions from the backtrace list in array in a newly
   malloc()ed memory block.  */
extern char **backtrace_symbols (void *const *__array, int __size)
     __throw __nonnull ((1));
/* this function is similar to backtrace_symbols() but it writes the result
   immediately to a file.  */
extern void backtrace_symbols_fd (void *const *__array, int __size, int __fd)
     __throw __nonnull ((1));
__end_decls
#endif /* execinfo.h  */

自定义异常类

backtraceexception.h

/*
 * backtraceexception.h
 *
 *  created on: 2018-6-10
 */
#ifndef backtraceexception_h_
#define backtraceexception_h_
#include 
#include 
class backtraceexception : public std::exception
{
public:
    backtraceexception(const std::string& msg);
    virtual ~backtraceexception()throw();
    const char* what()const throw()override;
    const char* stacktrace()const throw();
private:
    void fillstacktace();
private:
    std::string msg_;
    std::string stack_;
};
#endif /* backtraceexception_h_ */

backtraceexception.cpp

/*
 * backtraceexception.cpp
 *
 *  created on: 2018-6-10
 *      author:
 */
#include 
#include 
#include "backtraceexception.h"
backtraceexception::backtraceexception(const std::string& msg):msg_(msg) {
    fillstacktace();
}
backtraceexception::~backtraceexception() throw() {
    // todo auto-generated destructor stub
}
const char* backtraceexception::what()const throw(){
    return msg_.c_str();
}
const char* backtraceexception::stacktrace()const throw(){
    return stack_.c_str();
}
void backtraceexception::fillstacktace(){
      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)
        {
          stack_.append(strings[i]);
          stack_.push_back('\n');
        }
        free(strings);
      }
}

测试

#include "backtraceexception.h"
#ifdef test_backtrace_exception
#include 
int main()
{
    try{
        throw backtraceexception("this is an backtraceexception");
    }catch(backtraceexception &e){
        std::cout<
网站地图