• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

加班费的离开

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

C语言异常处理机制

   C语言异常处理

1.  常用方法

(1)使用abort()和exit()两个函数,他们声明在<stdlib.h>中;

(2)使用assert宏调用,它位于<assert.h>中。assert(expression)当expression为0时,就好引发abort();

(3)使用全局变量errno,它由C语言库www.fsshanqi.com函数提供,位于<errno.h>中;

(4)使用goto语用局部跳转到异常处理代码处;

(5)使用setjmp和longjmp实现全局跳转,它们声明<setjmp.h>中,一般由setjmp保存jmp_buf上下文结构体,然后由longjmp跳回到此时。

2.  实例演示

实例一 :使用exit()终止程序运行

#include<stdio.h>

#include<stdlib.h>

 

voidDivideError(void)

{

   printf("divide 0 error!\n");

}

doubledivide(double x,double y)

{

   if(y==0) exit(EXIT_FAILURE);//此时EXIT_FAILURE=1

//也可以使用atexit()函数来注册异常处理函数,但此时异常处理函//数必须形如voidfun(void);

   else return x/y;

}

intmain()

{

   double x,y,res;

  printf("x=");

  scanf("%lf",&x);

  printf("y=");

  scanf("%lf",&y);

  atexit(DivideError);

   res=divide(x,y);

   printf("result=%lf\n",res);

   return 0;

}

实例二:使用assert(expression)

#include<stdio.h>

#include<assert.h>

 

intmain()

{

   int a,b,res;

 res=scanf("%d,%d",&a,&b);

 //scnaf函数返回从stdin流中成功读入的数据个数

  assert(res==2); //如果res!=2,则出现异常

   return 0;

}

实例三:使用全局变量errno来获取异常情况的编号

#include<stdio.h>

#include<errno.h>

 

intmain()

{

   char filename[80];

   errno=0;

   scanf("%s",filename);

   FILE* fp=fopen(filename,"r");

   printf("%d\n",errno); //如果此时文件打不开,那么errno=2

   return 0;

}

实例四:使用goto实现局部跳转

#include<stdio.h>

#include<stdlib.h>

intmain()

{

   double x,y,res;

   int tag=0;

   if(tag==1)

   {

       Error:

       printf("divide0 error!\n");

       exit(1);

   }

  printf("x=");

  scanf("%lf",&x);

  printf("y=");

  scanf("%lf",&y);

   if(y==0)

   {

      tag=1;

      goto Error;

   }

   else

   {

     res=divide(x,y);

     printf("result =%lf\n",res);

   }

   return 0;

}

实例五:使用setjmp和longjmp实现全局跳转

#include<stdio.h>

#include<setjmp.h>

 

jmp_buf mark; //保存跳转点上下文环境的结构体

void DivideError()

{

   longjmp(mark,1);

}

 

intmain()

{

   double a,b,res;

   printf("a=");

   scanf("%lf",&a);

   printf("b=");

   scanf("%lf",&b);

   if(setjmp(mark)==0)

   {

      if(b==0) DivideError();

      else

      {

        res=a/b;

        printf("the result is%lf\n",res);

      }

   }

   else printf("Divide 0 error!\n");

   return 0;

}

三、     C++异常处理

1.     C++异常类的编写

#include<iostream>

#include<exception>

using namespacestd;

class DivideError:public exception //E从exception类派生而来

{

public:

      const char* what() //必须实现虚函数,它在exception类中定义,

//函数原型是 virtual const char* what() const throw()

      {

           return "除数为0错误\n";

      }

};

double divide(doublex,double y)

{

   if(y==0) throw DivideError(); //抛出异常

   else return x/y;

}

void main()

{

   double x,y;

   double res;

   try

      {

       cin>>x>>y;

       res=divide(x,y);

       cout<<res<<endl;

      }

      catch(DivideError& e)

      {

           cerr<<e.what();

      }

}

 

 

文章出自: 北京二型糖尿病症状    http://www.fstangniaobing.com

posted on 2012-08-06 09:31  加班费的离开  阅读(424)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3