Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2007, 6: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
Old Apr 4th, 2007, 8:09 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
The only way myclass::dosomething() can throw an exception specifying the line in main() where it is called is for the information to be passed from main(). The obvious way would be to pass them as arguments.

class myclass
{
    public:

          void dosomething(const char *, int);
}

void myclass::dosomething(const char *f, int c)
{
    throw XHandler("bad!!!",f,c);
}

int main()
{
     myclass c;
     try
     {
         c.dosomething(__FILE__, __LINE__);
     }
     catch(XHandler x) {
		// whatever 
	}
}

Incidentally, your declaration of the GetException() member function and your usage of it don't match.
grumpy is offline   Reply With Quote
Old Apr 4th, 2007, 9:35 AM   #3
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
Hey Grumpy!

Thanks for the reply!

Yeah sorry I typed that out quickly...

Well I guesss thats the solution... Bit messy tho! :/

Suppose we have a vector class and we want to divide by a scalar

class vector {
        public:
                ...
                vector operator/(float) const;
                ...
};

vector vector::operator/(float scalar) const {
        if(scalar == 0.0)
                throw XHandler("divide by zero");
        ...
}

int main() {
        vector v(3.0,3.34,8.3);
        try {
                vector newVec = v/0; //cause an exception - how to
                                                //find actual line where this occured???
        ...
        } catch(XHandler x) {
                ...
        }
};

this might seem a little stupid - but would be nice to find out exactly where the error occured... or maybe its just plain stupid!
rwm is offline   Reply With Quote
Old Apr 5th, 2007, 3:13 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
Getting a stack trace is the domain of debuggers, not the programming language. And it's a capability you won't normally need in production code. So it's hardly surprising that you have to jump through hoops to get a comparable capability in code.
grumpy is offline   Reply With Quote
Old Apr 10th, 2007, 2:35 AM   #5
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
yeah good point!
rwm is offline   Reply With Quote
Old Apr 10th, 2007, 5:17 AM   #6
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
sorry wrong post!
rwm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
exception handling l2u C++ 2 Mar 3rd, 2007 5:47 PM
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 1:35 AM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
exception handling chrome_knob Java 2 May 19th, 2005 5:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC