c语言中的异常处理机制setjmp和longjmp

/* -------------------------------------------------------------------------
** File : cexcept.c *
** Coder: Spark Song. *
** Note : Use the example code from 《C Interfaces and Implementations》 *
** -------------------------------------------------------------------------
*/


#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

int
Allocation_handled = 0;
jmp_buf Allocate_Failed;

void
*allocate(unsigned n)
{

void
* p = (void *)malloc(n);

if
(p)
return
p;

if
(Allocation_handled) /* 如果实例化了异常处理程序的话... */
longjmp(Allocate_Failed, 1); /* 产生异常,并抛出 */

assert(0); /* 如果没有实例化异常处理程序,则此断言会报出运行期的错误 */
}



int
main(int argc, char *argv[])
{

char
*buf = 0;
int
count = 0;


Allocation_handled = 1; /* 设置实例化异常的标志,设为1表示实例化了异常处理程序 */
if
(setjmp(Allocate_Failed)) /* 实例化异常 */
{

fprintf(stderr, "EXCEPT: Couldn't allocate the buffer\n");
exit(EXIT_FAILURE);
}


while
(1) /* 测试代码:一直分配内存,直到没有内存为止。没有内存时会触发异常 */
{

buf = (char *)allocate(4096000);
printf("Allocate successs, the count is: %d\n", count++);
}


Allocation_handled = 0;
return
0;
}


简要讲述一下代码的流程:
1.setjmp用来实例化异常处理程序,在这里我们的异常处理程序就是往stderr输出一个字符串并退出应用程序。

2.setjmp会返回2次值(颇有些fork()的味道)。setjmp第一次返回值是在应用代码(这里就是main函数里面)调用setjmp的地方,这时候它实例化了异常处理程序,并返
回0,所以异常处理程序的代码并没有被执行。在allocate中调用longjmp的时候,会引起setjmp第二次值的返回,此时的返回值由
longjmp的第二个参数所决定。文中我们调用longjmp的时候,传给它的第二个参数是1,所以setjmp返回时会执行if中的异常处理程序。


作者: cdlda   发布时间: 2010-11-11