Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎Lesson 7‎ > ‎

Sample Solutions

Practice 1

I just put it in the unnamed namespace so that it cannot be used from other source code.

#include <iostream>
using namespace std;

namespace {
    long div(long num, long div) throw(long) {
        if (div == 0)
            throw div;
        return num / div;
    }
}

Practice 2

When divide() function is called from main() function, it is not catching the exception, which may be thrown fromdivide(). When an exception is thrown and if it's not caught, terminate() function will be called. This terminate()function calls abort() function by default.

#include <iostream>
using namespace std;

namespace {
    long divide(long num, long div) throw(long) {
        if (div == 0)
            throw div;
        return num / div;
    }
}

int main() {
    long myarray1[] = {33400, 559990, 325, 5435, 225, 54235};
    long myarray2[] = {242, 13, 0, 35, 0, 55};
    int max = sizeof(myarray1) / sizeof(long);

    for (int i = 0; i < max; i ++) {
        try {
            cout << "(" << myarray1[i] << " / " << myarray2[i] << ") = ";
            cout << divide(myarray1[i], myarray2[i]) << endl;
        } catch (long ex) {
            cerr << "[long-type exception is thworn!]" << endl;
            cerr << "You cannot execute divide by zero." << endl;
        }
    }
    return 0;
}

Practice 3

#include <iostream>
#include <stdexcept>
using namespace std;

namespace {
    const int size = 4;
}

class SafeArray {
    // arrays for dividend and divisor
    long array1[size], array2[size];
public:
    // sets value to array
    void setdata(int index, long lnum, long ldiv) throw(range_error) {
        if (index < 0 || index >= size)
            throw range_error("index out of range!");
         array1[index] = lnum;
         array2[index] = ldiv;
    }
    // returns the result of the specified index
    long operator[](int index) throw(range_error, overflow_error) {
        if (index < 0 || index >= size)
            throw range_error("index out of range!");
        if (array2[index] == 0) throw overflow_error("divided by zero!");
        return array1[index] / array2[index];
    }
};

int main() {
    long array1[] = { 3950, 60000, 42000, 80000, 3240 };
    long array2[] = { 20, 870, 0, 500, 0 };
    int max = sizeof array1 / sizeof(long);

    SafeArray sarray;
    for (int i = 0; i < max + 1; i++) {
        try {
            // sets the value first.
            sarray.setdata(i, array1[i], array2[i]); 
            cout << "Division result: " << sarray[i] << endl;
        }
        // deal with range_error
        catch (range_error &re) {
            cerr << "\n\n[range_error exception is detected!]" << endl;
            cerr << re.what() << endl;
            exit(1);
        }
        // deal with overflow_error
        catch (overflow_error &oe) {
            cerr << "\n\n[overflow_error is detected!]" << endl;
            cerr << oe.what() << endl;
            exit(1);
        }
        // deal with exception 
        catch (exception) {
            cerr << "\n\n[exception is detected!]" << endl;
            exit(1);
        }

    }

    return 0;
}

Practice 4

Since you need to overload new operator for an array:

void* operator new[](size_t iSize) {
    void *buf;
    try {
        buf = malloc(iSize);
        if (buf == NULL)    throw bad_alloc();
    } catch (bad_alloc &ba) {
        throw ba;// of course this outer try-catch is redundant if you're going to throw it anyway...
    }

    return buf;
}
Comments