#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "excp.h"
static char* excp_get_msg(void);
static excpt_t* excp_get_excption(void);
int excp_create(excpt_list_t* pel)
{
pel->excp_ptr = NULL;
return 0;
}
void excp_set_try(void)
{
int stat = 0;
excpt_event_t* pev;
pev = (excpt_event_t*)malloc(sizeof(excpt_event_t));
if(stat != 0){
printf("Set Try Error.\n");
return;
}
memset(pev, 0, sizeof(excpt_event_t));
pev->prev = excp_list.excp_ptr;
excp_list.excp_ptr = pev;
}
void excp_clear(void)
{
excpt_event_t* ev;
if (excp_list.excp_ptr)
{
ev = excp_list.excp_ptr;
excp_list.excp_ptr = excp_list.excp_ptr->prev;
free(ev->excpt.msg);
free(ev);
}
}
void excp_throw(int type, const char* message, char* file, int line)
{
int stat = 0;
int len = 0;
int size = 0;
#ifndef NOEBUG
char buf[100];
#endif
#ifndef NOEBUG
if(type == 0){
return;
}
sprintf(buf, MESSAGE_FORMAT, file, line);
len = strlen(buf);
#endif
if(excp_list.excp_ptr == NULL ||
(excp_list.excp_ptr && excp_list.excp_ptr->excpt.msg != NULL)){
return;
}
size = strlen(message) + len +1;
excp_msg = (char*)malloc(sizeof(char) * EXCP_DEF_MSG_LEN);
if(excp_msg == NULL){
printf("Throw Error.\n");
return;
}
#ifndef NDEBUG
strcpy(excp_msg, buf);
strcat(excp_msg, message);
#else
strcpy(excp_msg, message);
#endif
if (excp_list.excp_ptr)
{
excp_list.excp_ptr->excpt.type = type;
excp_list.excp_ptr->excpt.msg = excp_msg;
excp_list.excp_ptr->excpt.srcfile = file;
excp_list.excp_ptr->excpt.line = line;
excp_list.excp_ptr->ev_idx = evThrow;
longjmp(excp_list.excp_ptr->buf, evThrow);
}
else
{
fprintf(stderr, excp_msg);
//abort();
}
}
void excp_throw_again(void)
{
excpt_t e;
if (excp_list.excp_ptr)
{
e = excp_list.excp_ptr->excpt;
if (excp_list.excp_ptr->prev)
{
excp_clear();
excp_list.excp_ptr->excpt = e;
longjmp(excp_list.excp_ptr->buf, evThrow);
}
else
{
fprintf(stderr, excp_msg);
abort();
}
}
}
int excp_get_error(int type)
{
if (excp_list.excp_ptr->ev_idx == evThrow &&
(type == EXCEPT_ALL || type == excp_list.excp_ptr->excpt.type))
{
excp_list.excp_ptr->ev_idx = evExcept;
return 1;
}
return 0;
}
void excp_set_end(void)
{
if (excp_list.excp_ptr && excp_list.excp_ptr->ev_idx == evThrow){
excp_throw_again();
}
excp_clear();
}
jmp_buf* excp_get_buf(void)
{
return &excp_list.excp_ptr->buf;
}
int excp_set_num(int idx)
{
excp_list.excp_ptr->ev_idx = idx;
return excp_list.excp_ptr->ev_idx;
}
static char* excp_get_msg(void)
{
return excp_msg;
}
// 获取当前异常结构
static excpt_t* excp_get_excption(void)
{
return &excp_list.excp_ptr->excpt;
}
static void fun(void* p){
if(p == NULL) Throw(1, "这个指针是空的呀!!!");
else printf("神马都是浮云!\n");
}
int main(){
void* p = NULL;
Try{
fun(p);
}
OnExcept(1){
printf("%s\n", Message);
}
Finally{
printf("这个指针是空的我有啥用呀?\n");
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用纯C语言实现异常处理
共2个文件
h:1个
c:1个
5星 · 超过95%的资源 需积分: 32 101 下载量 20 浏览量
2010-12-24
09:38:34
上传
评论
收藏 2KB RAR 举报
温馨提示
使用C语言实现的异常处理,上次上传的文件里有东西编译不过,是因为那些是项目中使用的一些封装,在此致歉。这次上传的是我新修改的,代码里有一个简单的例子。使用时候需要将main函数去掉
资源推荐
资源详情
资源评论
收起资源包目录
excp.rar (2个子文件)
excp
excp.c 3KB
excp.h 2KB
共 2 条
- 1
资源评论
- 临江仙_舵手2015-11-27资源不错,不过个人觉得还是不是很实用,因为在实践中,这个代码会带来效率和调试上的问题。
- mailyangj2014-10-27这个很好!
- Demon8152012-12-24感谢分享,对目前我需要编写的程序很有帮助。相当好的资源!
- channpc2011-12-15编码风格较规范,注释较详细,不错的资源。
主任大人
- 粉丝: 92
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功