![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! ![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
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! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
yeah good point!
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2
![]() |
sorry wrong post!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |