Linux 下 C++ 异常处理技巧
来源:互联网 发布:华微大数据营销平台 编辑:程序博客网 时间:2024/05/21 17:59
代码片段(3)
1. [代码]1. 在异常对象构造函数中生成一个堆栈跟踪
// Sample Program:
// Compiler: gcc 3.2.3 20030502
// Linux: Red Hat
#include <execinfo.h>
#include <signal.h>
#include <exception>
#include <iostream>
using
namespace
std;
/////////////////////////////////////////////
class
ExceptionTracer
{
public
:
ExceptionTracer()
{
void
* array[25];
int
nSize = backtrace(array, 25);
char
** symbols = backtrace_symbols(array, nSize);
for
(
int
i = 0; i < nSize; i++)
{
cout << symbols[i] << endl;
}
free
(symbols);
}
};
2. [代码]2. 将信号转换成异常
template
<
class
SignalExceptionClass>
class
SignalTranslator
{
private
:
class
SingleTonTranslator
{
public
:
SingleTonTranslator()
{
signal
(SignalExceptionClass::GetSignalNumber(), SignalHandler);
}
static
void
SignalHandler(
int
)
{
throw
SignalExceptionClass();
}
};
public
:
SignalTranslator()
{
static
SingleTonTranslator s_objTranslator;
}
};
// An example for SIGSEGV
class
SegmentationFault :
public
ExceptionTracer,
public
exception
{
public
:
static
int
GetSignalNumber() {
return
SIGSEGV;}
};
SignalTranslator<SegmentationFault> g_objSegmentationFaultTranslator;
// An example for SIGFPE
class
FloatingPointException :
public
ExceptionTracer,
public
exception
{
public
:
static
int
GetSignalNumber() {
return
SIGFPE;}
};
SignalTranslator<FloatingPointException> g_objFloatingPointExceptionTranslator;
3. [代码]3. 处理构造函数中的异常
class
ExceptionHandler
{
private
:
class
SingleTonHandler
{
public
:
SingleTonHandler()
{
set_terminate(Handler);
}
static
void
Handler()
{
// Exception from construction/destruction of global variables
try
{
// re-throw
throw
;
}
catch
(SegmentationFault &)
{
cout <<
"SegmentationFault"
<< endl;
}
catch
(FloatingPointException &)
{
cout <<
"FloatingPointException"
<< endl;
}
catch
(...)
{
cout <<
"Unknown Exception"
<< endl;
}
//if this is a thread performing some core activity
abort
();
// else if this is a thread used to service requests
// pthread_exit();
}
};
public
:
ExceptionHandler()
{
static
SingleTonHandler s_objHandler;
}
};
//////////////////////////////////////////////////////////////////////////
class
A
{
public
:
A()
{
//int i = 0, j = 1/i;
*(
int
*)0 = 0;
}
};
// Before defining any global variable, we define a dummy instance
// of ExceptionHandler object to make sure that
// ExceptionHandler::SingleTonHandler::SingleTonHandler() is invoked
ExceptionHandler g_objExceptionHandler;
A g_a;
//////////////////////////////////////////////////////////////////////////
int
main(
int
argc,
char
* argv[])
{
return
0;
}
0 0
- Linux 下 C++ 异常处理技巧
- Linux 下 C++ 异常处理技巧
- Linux 下 C++ 异常处理技巧
- Linux 下 C++ 异常处理技巧
- Linux 下 C++ 异常处理技巧
- Linux 下 C++ 异常处理技巧
- Linux 下 C++程序的异常处理技巧
- Linux 下 C++程序的异常处理技巧
- Linux 下 C++程序的异常处理技巧
- Linux C多线程异常处理
- linux 下 C 编程 C版的try catch 捕捉段错误和异常处理
- linux C 异常处理的方式
- LINUX下的C++异常处理
- linux下cron异常处理小结
- 异常处理的技巧
- Linux C 异常处理:绝对异常终止 exit()/abort()、assert()
- C语言下的setjmp longjmp(C 语言异常处理)
- linux下的C编程技巧
- 易读错音、易写错字
- 由GridView(RecyclerView、ListView)首项重复绘制引起的探究
- Spark Overview
- FFmpeg中的重要数据结构
- 关于归并排序及快速求序列逆序对数的学习
- Linux 下 C++ 异常处理技巧
- 百度地图KEY发布版SHA1和开发板SHA1如何获得
- JSP自定义标签开发入门
- IOS学习
- CAS操作ABA问题发生的场景
- 结合DiskLruCache和LruCache对图片进行缓存,防止OOM
- 创建一个类的时候java的执行顺序
- 英文歌曲:Battle Cry (变形金刚第四部主题曲)
- Python调用采用Boost Python封装的c++