Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8251082
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类: C/C++

2010-09-01 23:25:49

setjmp()和longjmp().使用setjmp()可以在程序中保存一个已知的无错误状态,一旦发生错误,就可以通过longjmp()返回到该状态。
示例:

#include <iostream>
#include <csetjmp>


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's no place like home" << endl;
    }
    longjmp(kansas,47);
}

int main()
{
    if (setjmp(kansas) == 0) {
        cout << "tornado, witch, muchkins..." << endl;
        oz();

    }
    else {
        cout << "Autie Em!"
             << " I had the strangest dream..."
             << endl;
    }
    return 0;
}


运行结果:

tornado, witch, muchkins...
Rainbow()
there's no place like home
there's no place like home
there's no place like home
Autie Em! I had the strangest dream...

Process returned 0 (0x0)   execution time : 0.094 s
Press any key to continue.

运行环境: codeblock 10 + gcc + winXP

阅读(2274) | 评论(1) | 转发(0) |
0

上一篇:关于WTL

下一篇:C++抛出一个异常

给主人留下些什么吧!~~

chinaunix网友2010-09-03 15:45:14

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com