Listing 15.15 shows the implementation of the methods that weren’t already defined inline in Listing 15.14. Note that nested classes require using the scope-resolution operator more than once. Also note that the operator[]() functions throw exceptions if the array index is out of bounds.

Listing 15.15. sales.cpp

// sales.cpp -- Sales implementation

#include "sales.h"

using std::string;

Sales::bad_index::bad_index(int ix, const string & s )

    : std::logic_error(s), bi(ix)

{

}

Sales::Sales(int yy)

{

    year = yy;

    for (int i = 0; i < MONTHS; ++i)

        gross[i] = 0;

}

Sales::Sales(int yy, const double * gr, int n)

{

    year = yy;

    int lim = (n < MONTHS)? n : MONTHS;

    int i;

    for (i = 0; i < lim; ++i)

        gross[i] = gr[i];

    // for i > n and i < MONTHS

    for ( ; i < MONTHS; ++i)

        gross[i] = 0;

}

double Sales::operator[](int i) const

{

    if(i < 0 || i >= MONTHS)

        throw bad_index(i);

    return gross[i];

}

double & Sales::operator[](int i)

{

    if(i < 0 || i >= MONTHS)

        throw bad_index(i);

    return gross[i];

}

LabeledSales::nbad_index::nbad_index(const string & lb, int ix,

           const string & s ) : Sales::bad_index(ix, s)

{

    lbl = lb;

}

LabeledSales::LabeledSales(const string & lb, int yy)

         : Sales(yy)

{

    label = lb;

}

LabeledSales::LabeledSales(const string & lb, int yy,

                           const double * gr, int n)

                                  : Sales(yy, gr, n)

{

    label = lb;

}

double LabeledSales::operator[](int i) const

{    if(i < 0 || i >= MONTHS)

        throw nbad_index(Label(), i);

    return Sales::operator[](i);

}

double & LabeledSales::operator[](int i)

{

    if(i < 0 || i >= MONTHS)

        throw nbad_index(Label(), i);

    return Sales::operator[](i);

}

Listing 15.16 uses the classes in a program that first tries to go beyond the end of the array in the LabeledSales object sales2 and then beyond the end of the array in the Sales object sales1. These attempts are made in two separate try blocks that test for each kind of exception.

Listing 15.16. use_sales.cpp

// use_sales.cpp  -- nested exceptions

#include

#include "sales.h"

int main()

{

    using std::cout;

    using std::cin;

    using std::endl;

    double vals1[12] =

    {

        1220, 1100, 1122, 2212, 1232, 2334,

        2884, 2393, 3302, 2922, 3002, 3544

    };

    double vals2[12] =

    {

        12, 11, 22, 21, 32, 34,

        28, 29, 33, 29, 32, 35

    };

    Sales sales1(2011, vals1, 12);

    LabeledSales sales2("Blogstar",2012, vals2, 12 );

    cout << "First try block:\n";

    try

    {

        int i;

        cout << "Year = " << sales1.Year() << endl;

        for (i = 0; i < 12; ++i)

        {

            cout << sales1[i] << ' ';

            if (i % 6 == 5)

                cout << endl;

        }

        cout << "Year = " << sales2.Year() << endl;

        cout << "Label = " << sales2.Label() << endl;

        for (i = 0; i <= 12; ++i)

        {

            cout << sales2[i] << ' ';

            if (i % 6 == 5)

                cout << endl;

        }

        cout << "End of try block 1.\n";

   }

   catch(LabeledSales::nbad_index & bad)

   {

        cout << bad.what();

        cout << "Company: " << bad.label_val() << endl;

        cout << "bad index: " << bad.bi_val() << endl;

   }

   catch(Sales::bad_index & bad)

   {

        cout << bad.what();

        cout << "bad index: " << bad.bi_val() << endl;

   }

   cout << "\nNext try block:\n";

   try

    {

        sales2[2] = 37.5;

        sales1[20] = 23345;

        cout << "End of try block 2.\n";

   }

   catch(LabeledSales::nbad_index & bad)

   {

        cout << bad.what();

        cout << "Company: " << bad.label_val() << endl;

        cout << "bad index: " << bad.bi_val() << endl;

   }

   catch(Sales::bad_index & bad)

   {

        cout << bad.what();

        cout << "bad index: " << bad.bi_val() << endl;

   }

   cout << "done\n";

    return 0;

}

Here is the output of the program in Listings 15.14, 15.15, and 15.16:

First try block:

Year = 2011

1220 1100 1122 2212 1232 2334

2884 2393 3302 2922 3002 3544

Year = 2012

Label = Blogstar

12 11 22 21 32 34

28 29 33 29 32 35

Index error in LabeledSales object

Company: Blogstar

bad index: 12

Next try block:

Index error in Sales object

bad index: 20

done

When Exceptions Go Astray

After an exception is thrown, it has two opportunities to cause problems. First, if it is thrown in a function that has an exception specification, it has to match one of the types in the specification list. (Remember that in an inheritance hierarchy, a class type matches objects of that type and of types descended from it.) If the exception doesn’t match the specification, the unmatched exception is branded an unexpected exception, and, by default, it causes the program to abort. (Although C++11 deprecates exception specifications, they still remain in the language and in some existing code.) If the exception passes this first hurdle (or avoids it because the function lacks an exception specification), it then has to be caught. If it isn’t, which can happen if there is no containing try block or no matching catch block, the exception is branded an uncaught exception, and by default, it causes the program to abort. However, you can alter a program’s response to unexpected and uncaught exceptions. Let’s see how, beginning with uncaught exceptions.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги