Hey,
I was wondering how to write a nice exception handling class...
ive written one along the lines of:
//this file - XHandler.h
class XHandler {
public:
XHandler(char *s,char *f,int l) ( //set string,file and line number }
~XHandler() { //free mem }
std::ostream &GetException() { //print error stuff }
private:
char *str;
char *file;
int line;
};
and I use it like:
//this file - myclass.h
#include "XHandler.h"
class myclass {
public:
...
void dosomething();
...
};
void myclass::dosomething() {
//something bad happened
throw XHandler("bad!!!",__FILE__,__LINE__);
...
}
in a source file i.e.
//this file - main.cpp
int main() {
myclass c;
c.dosomething(); //force to throw exception
//it would be nice to set the exception
//details according to this actual file
} catch(XHandler x) {
cout << x.GetException(cout);
}
return 0;
}
the obvious problem is it doesnt print the actual file and line where the exception was thrown - instead it returns the file it was declared in along with the line number - in that case the myclass.h file...
how can i get it to print the file and line number where the exception actually occured - i.e. inside the file that contains the main function (main.cpp)...
hope you can help!
