|
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:}; 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:}; Here is how this exception class is used in real life. CRefDynGenStrArray
a; a.Insert(CCStrWrap("sample"), 1); try { a.Erase(10);} catch (CDynGenStrArray_exc_bad_index &exc) { cout << "Exception test passed." << endl;} catch (...) { cout << "Exception test failed.";}; Result: Exception test passed. 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:}; Sample usage: // Let's jam reference table. string str;} catch (CRefDynGenStrArray_exc_invalid_ref_table &exc) { cout << "Exception test passed." << endl;} catch (...) { cout << "Exception test failed.";}; Result: Exception test passed. 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. |