c语言中的错误处理(setjump&longjmp用法实例)
2008-09-27 21:44
585 查看
#include<stdio.h>
#include<setjmp.h>
jmp_buf ebuf;
int func();
int main(){
int i;
printf("1111/n");
i = setjmp(ebuf);
printf("%d/n",i);
if(i==0){
func();
printf("this will not be printed");
}
if(i==3){
printf("3333/n");
}
printf("%d/n",i);
return 0;
}
int func(){
printf("2222/n");
longjmp(ebuf,3);
}
输出结果:
1111
0
2222
3
3333
3
相当与goto语句,当执行func函数时,由longjmp跳转到setjmp处,然后再往下执行。
注意的地方:longjmp(dbuf,val)其中的val不能为0,如果为0则系统默认再i=setjmp(ebuf)中i的返回值为1;
以下函数实现了多个函数之间的跳转,其中具体代码在 动态调用链接库中
#include <stdio.h>
#include <windows.h>
#include <setjmp.h>
jmp_buf ebuf;
int jump1();
int jump2();
int i;
main(){
i = setjmp(ebuf);
if(i==0|i==2){
jump1();
}
if(i==1){
jump2();
}
}
int jump1(){
while(1){
HINSTANCE hInstance;
void (*func)();
hInstance = LoadLibrary("my.dll");
showGUI();
char s[10];
scanf("%s",&s);
func = ( void (*)() )GetProcAddress(hInstance,s);
if(!func){
longjmp(ebuf,1);
}
(*func)();
continue;
}
}
int jump2(){
printf("your input is wrong/n");
longjmp(ebuf,2);
}
int showGUI(){
FILE *login;
char c;
login = fopen("login.txt","r");
if(!login){
printf("file err:login/n");
return;
}
while(1){
c = fgetc(login);
if(c == EOF){
break;
}
printf("%c",c);
}
fclose(login);
return 0;
}
#include<setjmp.h>
jmp_buf ebuf;
int func();
int main(){
int i;
printf("1111/n");
i = setjmp(ebuf);
printf("%d/n",i);
if(i==0){
func();
printf("this will not be printed");
}
if(i==3){
printf("3333/n");
}
printf("%d/n",i);
return 0;
}
int func(){
printf("2222/n");
longjmp(ebuf,3);
}
输出结果:
1111
0
2222
3
3333
3
相当与goto语句,当执行func函数时,由longjmp跳转到setjmp处,然后再往下执行。
注意的地方:longjmp(dbuf,val)其中的val不能为0,如果为0则系统默认再i=setjmp(ebuf)中i的返回值为1;
以下函数实现了多个函数之间的跳转,其中具体代码在 动态调用链接库中
#include <stdio.h>
#include <windows.h>
#include <setjmp.h>
jmp_buf ebuf;
int jump1();
int jump2();
int i;
main(){
i = setjmp(ebuf);
if(i==0|i==2){
jump1();
}
if(i==1){
jump2();
}
}
int jump1(){
while(1){
HINSTANCE hInstance;
void (*func)();
hInstance = LoadLibrary("my.dll");
showGUI();
char s[10];
scanf("%s",&s);
func = ( void (*)() )GetProcAddress(hInstance,s);
if(!func){
longjmp(ebuf,1);
}
(*func)();
continue;
}
}
int jump2(){
printf("your input is wrong/n");
longjmp(ebuf,2);
}
int showGUI(){
FILE *login;
char c;
login = fopen("login.txt","r");
if(!login){
printf("file err:login/n");
return;
}
while(1){
c = fgetc(login);
if(c == EOF){
break;
}
printf("%c",c);
}
fclose(login);
return 0;
}
相关文章推荐
- C语言错误处理——setjmp & longjmp
- C语言错误处理——setjmp & longjmp
- C语言错误处理——setjmp & longjmp
- GO语言标准错误处理机制error用法实例
- PHP错误处理函数set_error_handler()的用法
- PHP错误处理函数set_error_handler()的用法[转载]
- php自定义错误处理用法实例
- C语言异常处理——setjmp & longjmp
- 批处理中屏蔽错误信息 nul 2>nul 的用法 (cmd>nul空格2>nul)
- __autoload && set_hander php的错误异常处理机制以及自动加载机制
- C++中引用(&)的用法和应用实例-- Mr.xu
- 手工建库启动实例时一个小错误:ORA-00824: cannot set sga_target due to existing internal settings
- Android NDK编译错误处理-- error: undefined reference to '__dso_handle'
- 浅析C# get set的简单用法
- c语言异常处理之longjmp 和 setjmp
- Laravel中错误与异常处理的用法示例
- C语言-错误处理
- 大数据处理实例——Amazon商品评分&评论(二)
- C语言 字符串处理技巧(sscanf、strstr等用法)
- 【static&extern】c语言static用法