《C++ Primer Plus 6》15.3 异常

来源:互联网 发布:mac键盘大小写切换 编辑:程序博客网 时间:2024/06/08 07:36

《C++ Primer Plus 6》

15.3 异常

书中利用了求两个数的调和平均数为例子,这是求调和平均数的公式:

2.0 * x * y / (x + y)

显然,当两个数互为相反数时,求调和平均数的公式将被零除。

15.3.1 调用abort()

调用abort()函数,abort()函数位于头文件cstdlib中,其典型实现是向标准错误流(即cerr使用的错误流)发送消息abnormal program termination(程序异常终止,然后终止程序。它返回一个随实现而异的值,告知处理失败。

以下是使用abort()的小程序:

//error1.cpp -- using the abort() function#include <iostream>#include <cstdlib>double hmean(double a, double b);int main(){    double x, y, z;    std::cout << "Enter two numbers: ";    while (std::cin >> x >> y)    {        z = hmean(x, y);        std::cout            << "Harmonic mean of " << x << " and " << y            << " is " << z << std::endl;        std::cout << "Enter next set of numbers < q to qiut>: ";    }    std::cout << "Bye!\n";    system("pause");    return 0;}double hmean(double a, double b){    if (a == -b)    {        std::cout << "Untenable arguments to hmean()\n";        std::abort();    }    return 2.0 * a * b / (a + b);}

注意到,调用abort()后直接终止程序,而不是返回main()函数。
然而,依靠程序员来执行这种检查是不安全的。

15.3.2 返回错误码

使用函数指针的返回值来指出问题。
使用指针参数或引用参数将值返回给调用程序,通过返回值反映成功或者失败。

//error2.cpp -- returning an error code#include <iostream>#include <cfloat>bool hmean(double a, double b, double * ans);int main(){    double x, y, z;    std::cout << "Enter two numbers: ";    while (std::cin >> x >> y)    {        if (hmean(x, y, &z))            std::cout                << "Harmonic mean of " << x << " and " << y                << " is " << z << std::endl;        else            std::cout                << "One value should not be the negative "                << "of the other - try again.\n";        std::cout << "Enter next set of numbers <q to quit>: ";    }    std::cout << "Bye!\n";    system("pause");    return 0;}bool hmean(double a, double b, double * ans){    if (a == -b)    {        *ans = DBL_MAX;        return false;    }    else    {        *ans = 2.0 * a * b / (a + b);        return true;    }}

第三参数常使用指针,以此明显看出那个参数用于提供答案。
另一种方法是使用全局变量。

15.3.3 异常机制

异常提供了将控制权从程序的一个部分传递到另一部分的途径。对异常的处理有3个组成部分:

  • 引发异常;
  • 使用处理程序捕获异常;
  • 使用try块。

程序出现问题时将引发异常。
throw关键字表示引发异常,紧随其后的值指出了异常特征。
程序使用异常处理程序(exception handler)来捕获异常,其位于程序中。catch关键字表示捕获异常。处理程序以catch开头,随后是括号中的类型声明(throw紧随的类型);然后是代码块,采取措施。异常处理程序也被称为catch块。
try块标识期中特定的异常可能被激活的代码块,后面跟一个或多个catch。try块由关键字try指示,后面由一个花括号括起的代码块,表明需要注意这些代码引发的异常。

//error3.cpp -- using an excption#include <iostream>double hmean(double a, double b);int main(){    double x, y, z;    std::cout << "Enter two numbers: ";    while (std::cin >> x >> y)    {        try        {            z = hmean(x, y);        }        catch (const char * s)        {            std::cout << s << std::endl;            std::cout << "Enter a new pair of numbers: ";            continue;        }        std::cout            << "Harmonic mean of " << x << " and " << y            << " is " << z << std::endl;        std::cout << "Enter next set of numers <q to quit>: ";    }    std::cout << "Bye!\n";    system("pause");    return 0;}double hmean(double a, double b){    if (a == -b)        throw "bad hmean() arguments: a = -b not allowed";    return 2.0 * a * b / (a + b);}

如果try块中的某条语句引发异常,则catch块对其进行处理。
其中引发异常的是字符串“bad hmean() arguments: a = -b not allowed”。
执行throw语句即程序沿函数调用后退,直到找到包含try块的函数。在该程序中回到main()函数。
catch块是一个处理程序,char * s表明该处理程序和字符串异常匹配,并执行其中的代码,这里代码意义则容易理解。

0 0
原创粉丝点击