您的位置:首页 > 编程语言 > C语言/C++

从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介

2013-07-16 20:11 881 查看
一、C语言错误处理方法

1、返回值(if … else语句判断错误)
2、errno(linux 系统调用)
3、goto语句(函数内局部跳转)
4、setjmp、longjmp(Do not use setjmp and longjmp in C++ programs; these functions do not support C++ object semantics. )

#define _JBLEN 16

typedef _JBTYPE jmp_buf[_JBLEN];

Saves the current state of the program.

int setjmp(
jmp_buf env
);



Parameters
env
Variable in which environment is stored.


Return Value

Returns 0 after saving the stack environment. If setjmp returns as a result of alongjmp call, it returns thevalue argument oflongjmp, or if thevalue argument oflongjmp is 0,setjmp returns 1. There is no error return.

Restores stack environment and execution locale.

void longjmp(
jmp_buf env,
int value
);



Parameters
env
Variable in which environment is stored.

value
Value to be returned to setjmp call.

C++ Code
1
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

#include <stdio.h>

#include <setjmp.h>

jmp_buf buf;

double Divide(
double a,
double b)

{

if (b ==
0.
0)

{

longjmp(buf,
1);
// throw
}

else

return a / b;

}

int main(
void)

{

int ret;

ret = setjmp(buf);

if (ret ==
0)
// try
{

printf(
"division ...\n");

printf(
"%f\n", Divide(
5.
0,
0.
0));

}

else
if (ret ==
1)
// catch
{

printf(
"divisiong by zero\n");

}

return
0;

}



分析一下,首先setjump设置成功返回0,执行Divide函数,除数为0,longjump跳回到setjump处,返回参数1,故继续输出

divisiong by zero, setjump和longjump 已经是c++异常的雏形,即使Divide函数本身没有调用longjump,而是调用了某个函数,在这个函数内longjump,也可以跳转到setjump 处,这样就不用通过一层层的函数返回值来判断错误。

C语言的出错处理被认为是紧耦合的,函数的使用者必须在非常靠近函数调用的地方编写错误处理代码,这会使得其变得笨拙以及难以使用。

二、C++异常处理方法(throw, try, catch)

错误处理代码的编写不再冗长乏味,并且不再与“正常”代码混在一起。程序员可以将注意力集中于正常流程,然后在某个区域里编写异常处理代码。如果多次调用同一个函数,只需在一个地方编写一次错误处理代码。
错误不能被忽略。

C++ Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#include <iostream>

using
namespace std;

double Divide(
double a,
double b)

{

if (b ==
0.
0)

{

throw
1;
// throw
}

else

return a / b;

}

int main(
void)

{

try
// try
{

cout <<
"division ..." << endl;

cout << Divide(
3.
0,
1.
0) << endl;

cout << Divide(
5.
0,
0.
0) << endl;

}

catch (
int)
// catch
{

cout <<
"divisiong by zero" << endl;

}

return
0;

}



即无论在多远throw,都可以被catch到,但需要注意的是类型需要匹配,下面的文章将讨论抛出自定义类型的异常。

参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: