S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

C中的异常机制

Posted on 2010-04-16 23:28 S.l.e!ep.¢% 阅读(246) 评论(0)  编辑 收藏 引用 所属分类: C++

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

#define try if (!(ret = setjmp(buf)))
#define catch(i) else
#define throw(i) longjmp(buf, i)

jmp_buf buf;

void sub1()
{
    printf("in sub1()\n");
    throw(1);
    printf("you will never see this!");
}
void sub2()
{
    printf("in sub2()\n");
    throw(2);
    printf("you will never see this!");
}
int main()
{
    int ret;
    try
    {
        printf("first time through\n");
        sub1();
        sub2();
    }
    catch(ret)
    {
        switch(ret)
        {
            case 1:
                printf("sub1 raised an exception\n");
                break;
            case 2:
                printf("sub2 raised an exception\n");
                break;
            default:
                break;
        }
    }
    printf("back in main()\n");
    system("pause");
    return 0;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理