Intelligent Exception Handling

Actually, CRefDynGenStrArray implements basic form of exception handling: insert/erase function members perform index range checking whenever possible, in case of memory shortage operations which may corrupt memory forced to stop, etc. Now, think about this situation: a programmer who debug its code caught a generic exception thrown by a function member of CRefDynGenStrArray. How easy is to figure out what is going wrong without information like what parameters have been out of range?

Standard C++ Library defines a number of exception classes, all of which are derived from class exception. Function member virtual const char* what() usually returns string with generic description of failure (depends on realization, it may return nothing or just word "exception"). We will define our own exception classes which overload what() function member.

First one will be CDynGenStrArray_exc, which is base class for all other exception classes related to CDynGenStrArray and CRefDynGenStrArray. It contains data member mWhat of string type which used to record information about failure. throw() declaration after all function members of CDynGenStrArray_exc means that they cannot throw exception themselves.

class CDynGenStrArray_exc : public exception
{

protected:
string mWhat;

public:
CDynGenStrArray_exc() throw();
virtual const char* what(void) const throw();
};

CDynGenStrArray_exc_bad_index as name suggests is used to throw exception in case of bad index. Each function member of CDynGenStrArray and CRefDynGenStrArray which does insert or erase calls macro CDYNGENSTRARRAY_THROW_IFBADINDEX that checks index for validity and throws exception if necessary. Why we use macro instead of native function call? It is because with macro we can record source file name and line number, what is really helpful for debugging.

class CDynGenStrArray_exc_bad_index : public CDynGenStrArray_exc
{

public:
explicit CDynGenStrArray_exc_bad_index(long index, const char *cpp_source) throw();
};

Here is how this exception class is used in real life.

CRefDynGenStrArray a;

for (long i = 1; i < 5; ++i)

a.Insert(CCStrWrap("sample"), 1);

try {

a.Erase(10);

cout << "Exception test failed." << endl;
}
catch (CDynGenStrArray_exc_bad_index &exc) {
cout << "Exception test passed." << endl;
cout << "Exception cought: " << exc.what() << endl;
}
catch (...) {
cout << "Exception test failed.";
cout << "Unknown exception cought instead of CDynGenStrArray_exc_bad_index." << endl;
};

Result:

Exception test passed.
Exception cought: Exception - index is out of range: 10, Source File/Line# CRefDynGenStrArray.cpp(257):

Another exception class which checks indexes stored in reference table have been designed the same way.

class CRefDynGenStrArray_exc_invalid_ref_table : public CDynGenStrArray_exc
{

public:
explicit CRefDynGenStrArray_exc_invalid_ref_table(const CRefDynGenStrArray *arr, long rf_ind, long rl_ind,
const char *cpp_source) throw();
};

Sample usage:

// Let's jam reference table.
a.mRefTable[1] = 999;

try {

string str;
a(str, 1);
cout << "Exception test failed." << endl;
}
catch (CRefDynGenStrArray_exc_invalid_ref_table &exc) {
cout << "Exception test passed." << endl;
cout << "Exception cought: " << exc.what() << endl;
}
catch (...) {
cout << "Exception test failed.";
cout << "Unknown exception cought instead of CRefDynGenStrArray_exc_invalid_ref_table." << endl;
};

Result:

Exception test passed.
Exception cought: Exception - invalid entry in reference table: Reference Index# 1, Real Index# 999, Source File/Line# CRefDynGenStrArray.h(115):

Both code snippets are part of test suite class URefDynGenStrArrTest which is supplied with distribution.

You can download all source files from SourceForge.net. These sources are Copyright (c) by Andrei Verovski and are subject of the following license agreement.

For additional information, please refer to CRefDynGenStrArray Q&A and CRefDynGenStrArray Version History.

PS. Well, here I am finish my STL exercise part. Important subject currently omitted from this tutorial is "sorted associative containers" (map, multimap, set, multiset). However, if you completed this tutorial, you can learn yourself the rest of STL virtually in no time.

At this moment, I have no time to finish this article, especially its "Programming using Class Library" part. Sorry.


MacGuruHQ and MacGuruHQ Logo

Copyright© 1996 - 2002 by Andrei Verovski. All right reserved. Content may not be copied in whole or in part, through electronic or other means, without the author's permission. MacGuru Logo (blue penguin) by Sergey Orehov.