Chap.10: ERROR HANDLINGThere are two types of error situations that can happen in this library:
Each application has different requirements and style of error handling. For this reason, the library has centralized error handling which permits you to customize it by changing just a few lines in file LIB/MGR.H. The code here speaks better than a long explanation: // ---------- ERROR HANDLING FOR THE ENTIRE LIBRARY -----------------
#include <iostream.h>
class PTLerrorHandler{
public:
char *msg;
void *ptr;
PTLerrorHandler(char *m,void *p){msg=m; ptr=p;}
};
void PTLerror(char *msg,void *ptr){
cout << "ERROR: " << msg << " " << (unsigned long)ptr << "\n";
cout.flush();
throw PTLerrorHandler(msg,ptr);
}
void PTLwarning(char *msg,void *ptr){
cout << "WARNING: " << msg << " " << (unsigned long)ptr << "\n";
cout.flush();
}
In the case of any error, the library calls either PTLerror() or PTLwarning(), and these functions print the given message and the given address or other value. For example: MyObject *p; int i;
...
PTLerror("destroying object which participates in a collection, obj=",p);
...
PTLwarning("index already occupied, i=",(void*)i);
Function PTLerror() throws exception: throw PTLerrorHandler(msg,ptr); It just depends on you how you will handle library errors. You can use the mechanism supplied with the library and catch the exceptions: try {
... your normal code
}
catch(PTLerrorHandler& eh){
... eh.msg has the message
... eh.ptr has the pointer or other value
}
If you don't want to abort the run even the in case of a serious error, comment out the line which throws the exception. If you want the messages on a different I/O stream, replace cout in functions PTLerror() and PTLwarning(). If you don't want to see any message, comment out the two lines that print them. If you want something completely different, supply new source for functions PTLerror() and PTLwarning(). Note that some compilers require a special option for stack unwinding. For example Microsoft Visual C++ must be compiled with -GX flag..., while the Borland C++ compiler does not require any special option. If you plan to catch the exceptions, this option must be added to the appropriate file PTL\ENVIR\*.cmp, for example file msft.cmp. This is the end of the last Chapter. |