CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 20th, 2009, 02:57 PM
    qwertygeek qwertygeek is offline
    Member
     
    Join Date: Jan 2009
    Posts: 37
    qwertygeek is an unknown quantity at this point (<10)
    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;
    }
    And this way:

    Code:
    try {
       // statements
    }
    catch (InvalidTokenException ex) {
      throw InvalidTokenException( ex.getError() );  // basically const new one out of old one
    }
    I can't see any need to use the second method but it was a test after the first method failed.
    Does anyone have any suggestions as to why this isn't working?
    Reply With Quote
      #2    
    Old November 20th, 2009, 03:09 PM
    laserlight laserlight is offline
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    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) {}
    };
    I have chosen not to provide a default constructor because it could lead to the kind of "lost" message that you see if an exception is accidentally default constructed. Now you would use it as:
    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
    Reply With Quote
      #3    
    Old November 20th, 2009, 03:18 PM
    qwertygeek qwertygeek is offline
    Member
     
    Join Date: Jan 2009
    Posts: 37
    qwertygeek is an unknown quantity at this point (<10)
    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...
    Reply With Quote
      #4    
    Old November 20th, 2009, 03:27 PM
    laserlight laserlight is offline
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: Custom exception class - variable value becoming lost?

    Quote:
    Originally Posted by qwertygeek
    I'll take a look at the runtime_error. What's the difference other than the name?
    As you can see from my example, it requires less boilerplate code to setup since you just need to define the constructor without bothering to override what() yourself. As a side effect, you actually get to use what() instead of overriding it in a not so useful way.

    Quote:
    Originally Posted by qwertygeek
    (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..)
    Sure, but remember to catch std::exception at some point, and possibly also the catch-all (...).
    __________________
    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
    Reply With Quote
      #5    
    Old November 20th, 2009, 04:00 PM
    qwertygeek qwertygeek is offline
    Member
     
    Join Date: Jan 2009
    Posts: 37
    qwertygeek is an unknown quantity at this point (<10)
    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...
    Reply With Quote
      #6    
    Old November 20th, 2009, 04:24 PM
    laserlight laserlight is offline
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: Custom exception class - variable value becoming lost?

    Quote:
    Originally Posted by qwertygeek
    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...
    Yes.
    __________________
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    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


    All times are GMT -5. The time now is 02:54 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.