Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   exception handling class (http://www.programmingforums.org/showthread.php?t=12935)

rwm Apr 4th, 2007 7:23 AM

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!

:D

grumpy Apr 4th, 2007 9:09 AM

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.

rwm Apr 4th, 2007 10:35 AM

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!

grumpy Apr 5th, 2007 4:13 AM

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.

rwm Apr 10th, 2007 3:35 AM

yeah good point!

rwm Apr 10th, 2007 6:17 AM

sorry wrong post!


All times are GMT -5. The time now is 2:03 AM.

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