出错处理
2013-12-07 18:29
148 查看
一. c语言出错处理
1. 通过函数返回值获得出错信息、通过标准C语言中的errno()和perror()函数获得错误信息
2. 可利用C标准库中的信号处理系统,利用signal()函数(判断事件发生类型)和raise()函数(产生事件),例如:
#include <signal.h>
#include <stdio.h>
void catch_function(int);
int main()
{
if (signal(SIGINT, catch_function) == SIG_ERR)
{
printf("signal(SIGINT, catch_function) fail!\n");
return 1;
}
printf("raise(SIGING)\n");
if (raise(SIGINT) != 0)
{
printf("raise(SIGINT) faile !\n");
return 1;
}
printf("return\n");
getchar();
return 0;
}
void catch_function(int signal)
{
printf("catch error %d", signal);
}
3. 使用C标准库中非局部跳转函数:setjmp()和longjmp(),例如:
#include <iostream>
#include <setjmp.h>
using namespace std;
class rainbow
{
public:
rainbow()
{
cout << "rainbow()" << endl;
}
~rainbow()
{
cout << "~rainbow()" << endl;
}
};
jmp_buf kansas;
void OZ()
{
rainbow RB;
for (int i = 0; i < 3; i++)
{
cout << "there is no place like home\n";
}
longjmp(kansas, 47);
cout << "cannot be print" << endl;
}
int main()
{
if (setjmp(kansas) == 0)
{
cout << "tornado, witch, munchkins...\n";
OZ();
}
else
{
cout << "Auntie em! " << "I had the strangest dream..." << endl;
}
getchar();
return 0;
}
二. c++异常处理
在c++中,C的错误处理方式不太使用,因为他们不会调用对象的析构函数
1. 如果异常类存在继承关系,catch的时候应该使用引用,因为存在对象切片问题,例如:
#include <iostream>
using namespace std;
class base
{
public:
virtual void what()
{
cout << "base" << endl;
}
};
class derived : public base
{
public:
void what()
{
cout << "derived" << endl;
}
};
void f()
{
throw derived();
}
int main()
{
//如果是值传递,会存在对象切片问题
try
{
f();
}
catch(base b)
{
b.what();
}
//需要修改为引用传递
try{
f();
}
catch(base& b)
{
b.what();
}
system("pause");
return 0;
}
1. 通过函数返回值获得出错信息、通过标准C语言中的errno()和perror()函数获得错误信息
2. 可利用C标准库中的信号处理系统,利用signal()函数(判断事件发生类型)和raise()函数(产生事件),例如:
#include <signal.h>
#include <stdio.h>
void catch_function(int);
int main()
{
if (signal(SIGINT, catch_function) == SIG_ERR)
{
printf("signal(SIGINT, catch_function) fail!\n");
return 1;
}
printf("raise(SIGING)\n");
if (raise(SIGINT) != 0)
{
printf("raise(SIGINT) faile !\n");
return 1;
}
printf("return\n");
getchar();
return 0;
}
void catch_function(int signal)
{
printf("catch error %d", signal);
}
3. 使用C标准库中非局部跳转函数:setjmp()和longjmp(),例如:
#include <iostream>
#include <setjmp.h>
using namespace std;
class rainbow
{
public:
rainbow()
{
cout << "rainbow()" << endl;
}
~rainbow()
{
cout << "~rainbow()" << endl;
}
};
jmp_buf kansas;
void OZ()
{
rainbow RB;
for (int i = 0; i < 3; i++)
{
cout << "there is no place like home\n";
}
longjmp(kansas, 47);
cout << "cannot be print" << endl;
}
int main()
{
if (setjmp(kansas) == 0)
{
cout << "tornado, witch, munchkins...\n";
OZ();
}
else
{
cout << "Auntie em! " << "I had the strangest dream..." << endl;
}
getchar();
return 0;
}
二. c++异常处理
在c++中,C的错误处理方式不太使用,因为他们不会调用对象的析构函数
1. 如果异常类存在继承关系,catch的时候应该使用引用,因为存在对象切片问题,例如:
#include <iostream>
using namespace std;
class base
{
public:
virtual void what()
{
cout << "base" << endl;
}
};
class derived : public base
{
public:
void what()
{
cout << "derived" << endl;
}
};
void f()
{
throw derived();
}
int main()
{
//如果是值传递,会存在对象切片问题
try
{
f();
}
catch(base b)
{
b.what();
}
//需要修改为引用传递
try{
f();
}
catch(base& b)
{
b.what();
}
system("pause");
return 0;
}
相关文章推荐
- 【技术贴】虚拟机安装出错处理。
- mssqlserver 2008 r2 附加数据库对于服务器失败 处理数据库日志文件时出错
- 记录Android Studio项目提交到github上的出错处理
- 学习Altas 笔记[js调用重载的方法出错,如何处理]
- Ubuntu9.0.4下安装SVN插件出错处理
- Eclipse单步debug J2ME 出错的处理
- linux下运行《UNIX环境高级编程》的第一个程序时源码编译出错的处理方法
- thinkphp3.2.3编译时new model()出错的处理方法
- win32下安装postgresql9.0以上版本出错处理
- opencv中图像处理出错情况总结
- 如何利用PHP自定义错误处理器处理出错信息?
- dpkg:处理 xxx (--configure)时出错解决办法(转载)
- maven依赖本地非repository中的jar包-依赖jar包放在WEB-INF/lib等目录下的情况客户端编译出错的处理
- 无法显示XML页--名称以无效字符开头。处理资源“http://xxxxxx”时出错的解决办法
- WINDOWS下OEM启动出错处理一例
- dpkg:处理 xxx (--configure)时出错解决办法
- vs2010编译thrift之compiler出错的处理方法
- Java一些常见的出错异常处理
- 删除表中含有默认值的列提示出错的一种处理方式
- 出错处理函数abort、exit、atexit、strerror. . .