C语言实现异常处理(使用 setjmp 和 longjmp 函数)
来源:互联网 发布:mfc编程实例 编辑:程序博客网 时间:2024/05/18 09:17
C语言实现异常处理(使用 setjmp 和 longjmp 函数)
废话少说,直接贴上 MSDN 的实例代码,如果看不懂说明你水平不行 :)
// crt_fpreset.c// This program uses signal to set up a// routine for handling floating-point errors.#include <stdio.h>#include <signal.h>#include <setjmp.h>#include <stdlib.h>#include <float.h>#include <math.h>#include <string.h>jmp_buf mark; // Address for long jump to jump toint fperr; // Global error numbervoid __cdecl fphandler( int sig, int num ); // Prototypesvoid fpcheck( void );int main( void ){ double n1 = 5.0; double n2 = 0.0; double r; int jmpret; // Unmask all floating-point exceptions. _control87( 0, _MCW_EM ); // Set up floating-point error handler. The compiler // will generate a warning because it expects // signal-handling functions to take only one argument. if( signal( SIGFPE, (void (__cdecl *)(int)) fphandler ) == SIG_ERR ) { fprintf( stderr, "Couldn't set SIGFPE\n" ); abort(); } // Save stack environment for return in case of error. First // time through, jmpret is 0, so true conditional is executed. // If an error occurs, jmpret will be set to -1 and false // conditional will be executed. jmpret = setjmp( mark ); if( jmpret == 0 ) { printf( "Dividing %4.3g by %4.3g...\n", n1, n2 ); r = n1 / n2; // This won't be reached if error occurs. printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r ); r = n1 * n2; // This won't be reached if error occurs. printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r ); } else fpcheck(); system("pause"); //[]}// fphandler handles SIGFPE (floating-point error) interrupt. Note// that this prototype accepts two arguments and that the // prototype for signal in the run-time library expects a signal // handler to have only one argument.//// The second argument in this signal handler allows processing of// _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and // _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols // that augment the information provided by SIGFPE. The compiler // will generate a warning, which is harmless and expected.void fphandler( int sig, int num ){ // Set global for outside check since we don't want // to do I/O in the handler. fperr = num; // Initialize floating-point package. */ _fpreset(); // Restore calling environment and jump back to setjmp. Return // -1 so that setjmp will return false for conditional test. longjmp( mark, -1 );}void fpcheck( void ){ char fpstr[30]; switch( fperr ) { case _FPE_INVALID: strcpy_s( fpstr, sizeof(fpstr), "Invalid number" ); break; case _FPE_OVERFLOW: strcpy_s( fpstr, sizeof(fpstr), "Overflow" ); break; case _FPE_UNDERFLOW: strcpy_s( fpstr, sizeof(fpstr), "Underflow" ); break; case _FPE_ZERODIVIDE: strcpy_s( fpstr, sizeof(fpstr), "Divide by zero" ); break; default: strcpy_s( fpstr, sizeof(fpstr), "Other floating point error" ); break; } printf( "Error %d: %s\n", fperr, fpstr );}
执行结果:
Dividing 5 by 0...
Error 131: Divide by zero
注: Linux系统也有 setjmp 和 longjmp 函数
0 0
- C语言实现异常处理(使用 setjmp 和 longjmp 函数)
- C语言利用setjmp和longjmp实现异常处理
- c语言中的异常处理,setjmp 与 longjmp函数
- c语言异常处理之longjmp 和 setjmp
- C语言中利用setjmp和longjmp做异常处理
- C语言中利用setjmp和longjmp做异常处理
- C语言中利用setjmp和longjmp做异常处理
- C语言实现异常机制setjmp() longjmp()
- C异常处理机制:setjmp和longjmp
- C异常处理机制:setjmp和longjmp
- C异常处理机制:setjmp和longjmp
- C异常处理机制:setjmp和longjmp
- C语言中setjmp和longjmp函数
- C语言中的setjmp和longjmp函数
- C语言下的setjmp longjmp(C 语言异常处理)
- setjmp和longjmp处理异常
- 全面了解setjmp与longjmp(C语言异常处理机制)
- C语言异常处理机制, setjmp() and longjmp()
- Spring @RequestMapping的用法
- 依赖属性实验案例一
- Android中的Shape使用总结
- ButterKnife使用
- 写了10+年的VC,现在根据公司需要,从头学习用C#做网站,用这里记下学习过程中的点点滴滴吧
- C语言实现异常处理(使用 setjmp 和 longjmp 函数)
- <弹弹弹>现已提交苹果申请,尽请期待!
- cocos2dx游戏开发之利用多点触摸(实现缩放功能或者简单的手势识别)
- LK光流算法中是怎么使用金字塔的(就一个截图,大概明白点就行,毕竟有函数不必自己实现)
- Android崩溃处理
- 使用truelicense进行Java程序license控制 经过扩张可以验证license 开始结束日期,验证绑定给定mac地址
- 如何选择服务器的位置?
- 三款新品亮相家博会,卡萨帝新理念获业界认同
- UNP在linux上环境搭建