Exception类实现(12)
来源:互联网 发布:高考状元笔记淘宝 编辑:程序博客网 时间:2024/06/07 16:38
Exception 类图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-message_: string
-stack_:string
<<create>>-Exception(what:
char
)
<<create>>-Exception(what:string)
<<destroy>>-Exception()
+what():
const
char
*
+stackTrace():
const
char
*
-fillStackTrace ():
void
下面是源代码:
头文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MUDUO_BASE_EXCEPTION_H
#define MUDUO_BASE_EXCEPTION_H
#include <muduo/base/Types.h>
#include <exception>
namespace
muduo
{
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 // MUDUO_BASE_EXCEPTION_
源文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#include <muduo/base/Exception.h>
//#include <cxxabi.h>
#include <execinfo.h>
#include <stdlib.h>
using
namespace
muduo;
Exception::Exception(
const
char
* msg)
: message_(msg)
{
fillStackTrace();
}
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];
/*
backtrace() returns a backtrace for the calling program, in the array pointed to by buffer. A backtrace is the series of currently
active function calls for the program. Each item in the array pointed to by buffer is of type void *, and is the return address from
the corresponding stack frame. The size argument specifies the maximum number of addresses that can be stored in buffer.
*/
//buffer 存地就是函数的地址
int
nptrs = ::backtrace(buffer, len);
/*
Given the set of addresses returned by backtrace() in buffer, backtrace_symbols() translates the addresses into an array of strings
that describe the addresses symbolically. The size argument specifies the number of addresses in buffer. The symbolic representation
of each address consists of the function name (if this can be determined), a hexadecimal offset into the function, and the actual
return address (in hexadecimal). The address of the array of string pointers is returned as the function result of backtrace_sym-
bols(). This array is malloc(3)ed by backtrace_symbols(), and must be freed by the caller. (The strings pointed to by the array of
pointers need not and should not be freed.)
Pointer--------pointer1 ------string
--------------pointer2 ------string
--------------pointer3 ------string
--------------pointer4 ------string
--------------pointer5 ------string
--------------pointer6 ------string
我们要释放pointer占用的内存,但是pointer所指向的内存不需要我们释放
free (Pointer) 就OK了
*/
//将buffer里面的地址转换为函数符号,也可以所说函数的名字
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);
}
}
测试程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <muduo/base/Exception.h>
#include <stdio.h>
class
Bar
{
public
:
void
test()
{
throw
muduo::Exception(
"oops"
);
}
};
void
foo()
{
Bar b;
b.test();
}
int
main()
{
try
{
foo();
}
catch
(
const
muduo::Exception& ex)
{
printf
(
"reason: %s\n"
, ex.what());
printf
(
"stack trace: %s\n"
, ex.stackTrace());
}
}
程序输出:
1
2
3
4
5
6
7
8
reason: oops
stack trace: muduo::Exception::fillStackTrace()
muduo::Exception::Exception(
char
const
*)
Bar::test()
foo()
./exception_test(main+0x10)
/lib/libc.so.6(__libc_start_main+0xe6)
./exception_test()
- Exception类实现(12)
- exception类的实现
- exception 的实现
- Exception类
- Exception类
- 借助backtrace和demangle实现异常类Exception
- 借助backtrace和demangle实现异常类Exception
- Java常用工具类(一)Exception
- ART如何实现Exception.printStackTrace
- Java Review 12 Exception
- Java Review 12 Exception
- 异常类exception classes
- Exception异常类(C#)
- java Exception 类
- exception工具类
- C++ exception类
- C++ exception类
- Exception 类的层次
- strlen(str)与str.length()
- android Activity的生命周期于四种加载状态
- cookie不能获取自己设置的,只能获取系统的名字为JSESSIONID的cookie的解决办法
- MVC的含义,并说明其各部分通常使用的Java技术
- 关于tclsh 中环境变量(env)的使用
- Exception类实现(12)
- android 获取手机信息,并且将结果post到服务器和保存到本地
- [LeetCode] Subsets
- linux手工配置ip
- jquery : 获取可视窗体高度
- Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient va
- QQ登陆画面的最小化显示机制(使程序不在任务栏显示)
- 设计模式:模板方法模式(8)C++版
- 修改类属性