zoukankan      html  css  js  c++  java
  • C++_异常4-将对象用作异常类型

    通常,引发异常的函数将传递一个对象。这样做的重要优点之一就是,可以利用不同的异常类型来区分不同的函数在不同的情况下引发的异常

    对象可以携带信息,程序员可以根据这些信息来确定异常的原因。

    同时,catch块可以根据这些信息来决定采取什么样的措施。

     

    下面是针对函数引发的异常而提供的一种设计:

     1 class bad_hmean
     2 {
     3 private:
     4     double v1;
     5     double v2;
     6 
     7 public:
     8     bad_hmean(int a =0, int b=0):v1(a), v2(b) {}
     9     void mesg();
    10 };
    11 
    12 inline void bad_hmean::mesg()
    13 {
    14     std::cout<<"hmean("<<v1<<","<<v2<<"):"
    15            <<"invalid arguments: a = -b
    ";
    16 
    17 }

    可以将bad_hmean对象初始化为传递给含税hmean()的值,而方法mesg()可用于报告问题。

    函数hmean()可以使用如下代码:

    1 if (a == -b)
    2     throw bad_hmean(a,b);

    上述代码调用构造函数bad_hmean(),以初始化对象,使其存储参数值。

     

    接下来看一个新的例子:

    添加了一个新的异常类bad_gmean,类bad_gmean中的函数gmean()计算两个数的几何平均值,即乘积的平方根。

     1 //exc_exam.h  --  exception classes for hmean(), gmean()
     2 #include <iostream>
     3 
     4 class bad_hmean()
     5 {
     6 private:
     7     double v1;
     8     double v2;
     9 
    10 public:
    11     bad_hmean(double a=0, double b=0) : v1(a), v2(b){}
    12     void mesg();
    13 };
    14 
    15 inline void bad_hmean::mesg()
    16 {
    17     std::cout<<"hmean("<<v1<<","<<v2<<"):"
    18                   <<"invalid arguments:a=-b
    ";
    19 }
    20 
    21 class bad_gmean()
    22 {
    23     double v1;
    24     double v2;
    25     bad_gmean(double a=0, double b=0):v1(a), v2(b){}
    26     const char * mesg();
    27 };
    28 
    29 
    30 inline const char * bad_gmean::mesg()
    31 {
    32     return "gmean() arguments should be >=0
    ";
    33 }
     1 //error4.cpp - using exception classes
     2 #include <iostream>
     3 #include <cmath>
     4 #include "exc_mean.h"
     5 
     6 double hmean(double a, double b);
     7 double gmean(double a, double b);
     8 
     9 int main()
    10 {
    11     using std::cout;
    12     using std::cin;
    13     using std::endl;
    14 
    15     double x,y,z;
    16 
    17     cout<<"Enter two numbers: ";
    18     while(cin>>x>>y)
    19     {
    20         try {
    21             z = hmean(x,y);
    22             cout<<"Harmonic mean of"<<x<<" and "<<y
    23                    <<" is "<<z<<endl;
    24             cout<<"Geometric mean of "<<x<<" and "<<y
    25                    <<" is "<<gmean(x,y)<<endl;
    26             cout<<"Enter next set of numbers <q to quit>:";
    27         }
    28     }
    29     catch(bad_hmean & bg)
    30     {
    31         bg.mesg();
    32         cout<<"Try again.
    ";
    33         continue;
    34     }
    35     catch(bad_gmean & hg)
    36     {
    37         bg.mesg();
    38         cout<<"Value used: "<<hg.v1<<", "
    39                <<hg.v2<<end;
    40         cout<<"Sorry, you don't get to play any more.
    ";
    41         break;
    42     }
    43 
    44     cout<<"Bye!
    ";
    45     return 0;
    46 }
    47 
    48 double hmean();
    49 {
    50     if(a ==-b)
    51         throw bad_hmean(a,b);
    52     return 2.0*a*b/(a+b);
    53 }
    54 
    55 double gmean();
    56 {
    57     if(a<0||b<0)
    58         throw bad_gmean(a,b);
    59     return std::sqrt(a*b);
    60 }

    bad_hmean()和bad_gmean()使用的技术不一样,

    bad_gmean()使用的是公有方法和公有数据,该方法返回一个C-风格字符串;

  • 相关阅读:
    PAT (Advanced Level) 1010. Radix (25)
    PAT (Advanced Level) 1009. Product of Polynomials (25)
    PAT (Advanced Level) 1008. Elevator (20)
    PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
    PAT (Advanced Level) 1006. Sign In and Sign Out (25)
    PAT (Advanced Level) 1005. Spell It Right (20)
    PAT (Advanced Level) 1004. Counting Leaves (30)
    PAT (Advanced Level) 1001. A+B Format (20)
    PAT (Advanced Level) 1002. A+B for Polynomials (25)
    PAT (Advanced Level) 1003. Emergency (25)
  • 原文地址:https://www.cnblogs.com/grooovvve/p/10435104.html
Copyright © 2011-2022 走看看