View Single Post
Old Apr 4th, 2007, 7:23 AM   #1
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
exception handling class

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!

rwm is offline   Reply With Quote