| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Custom exception class - variable value becoming lost?
Okay, so I'm working on a small application for which I have a custom Exception class, defined as such:
Code:
class InvalidTokenException : public exception {
string error;
public:
InvalidTokenException() {};
~InvalidTokenException() throw() {};
InvalidTokenException(string s) {
this->error = s;
}
string getError(){
return error;
}
virtual const char* what() const throw()
{
return "Invalid Token Exception";
}
};
The program reads through a text file, tokenizing input. If it comes across an invalid token one of these gets generated and thrown. Because of the recursive nature of tokenizing, an exception frequently has to be thrown, caught, and thrown again. The weird problem I'm having is this: depending on where in the program I throw the exception, the string s becomes lost. I have debug prints in every catch block that catches one of these and prints ex.getError() before throwing it again, and depending on where I throw the error, I see this: Code:
Opened file : ../../../doc/input.txt for parsing... ******** Caught invalid token exception in GetNextToken... Error was: Invalid Data Token near: 1w ******** Caught invalid token exception in ParseBodyToken: Error was: Invalid Data Token near: 1w ******** Caught invalid token exception in ParseProgToken: Error was: Invalid Data Token near: 1w ******** Caught invalid token exception in GetNextToken... Error was: Invalid Data Token near: 1w Invalid token. Invalid Parse Data Token near: 1w Exiting... However, if I move the invalid token just ONE token further ahead in the input text file... Code:
Opened file : ../../../doc/input.txt for parsing... ******** Caught invalid token exception in GetNextToken... Error was: ******** Caught invalid token exception in Parse Data Token... Error was: ******** Caught invalid token exception in GetNextToken... Error was: ******** Caught invalid token exception in ParseBodyToken: Error was: ******** Caught invalid token exception in ParseProgToken: Error was: ******** Caught invalid token exception in GetNextToken... Error was: Invalid token. Exiting... I don't understand where my token is becoming lost for a generic token.. I'm building it with a specific string anywhere that it's generated. I've tried doing it this way: Code:
try {
// statements
}
catch (InvalidTokenException ex) {
throw ex;
}
Code:
try {
// statements
}
catch (InvalidTokenException ex) {
throw InvalidTokenException( ex.getError() ); // basically const new one out of old one
}
Does anyone have any suggestions as to why this isn't working? |
|
#2
|
|||
|
|||
|
Re: Custom exception class - variable value becoming lost?
Are you sure that the string is "lost" rather than simply empty?
I suggest that instead of deriving from std::exception, you derive from std::runtime_error: Code:
class InvalidTokenException : public std::runtime_error {
public:
explicit InvalidTokenException(const std::string& message)
: std::runtime_error(message) {}
};
Code:
try {
// ...
} catch (const std::exception& e) {
// Log the exception? Let's print its message as an example.
std::cout << e.what() << std::endl;
throw; // re-throw the same exception.
}
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
|
#3
|
|||
|
|||
|
Re: Custom exception class - variable value becoming lost?
It shouldn't be empty as the token that's being parsed is of the same type as the one that successfully prints the output so it's being built in the same place - that being ParseDataToken() - which throws if it encounters something invalid. To be safe I went back and removed the default constructor, and added distinct strings to each of my throw statements. Still coming up empty on ex.getError(). Something very odd going on yes?
I'll take a look at the runtime_error. What's the difference other than the name? (and I can't just catch std::exception, I have to stay a little specific as I have other exceptions to be caught as well..) runtime_error is definitly a consideration but I'm really confused as to what's up with C++ and the Disappearing String here... |
|
#4
|
|||
|
|||
|
Re: Custom exception class - variable value becoming lost?
Quote:
Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
|
#5
|
|||
|
|||
|
Re: Custom exception class - variable value becoming lost?
Alright... it was evidently my stringstream... but about this runtime_error, the argument I pass in I assume will get assigned to what prints out when you call what()? That does sound awfully simpler...
|
|
#6
|
|||
|
|||
|
Re: Custom exception class - variable value becoming lost?
Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|