deriving from std::exception

This is a discussion on deriving from std::exception within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I want to derive some exception classes from std::exception, which serve as bug-hunting devices with specific messages. But ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    deriving from std::exception

    Hi everyone,

    I want to derive some exception classes from std::exception, which serve as bug-hunting devices with specific messages. But the screen output seems really awkward to me. An example:

    Code:
    class VectorException: public exception
    {
     public:
     VectorException() : message( (string)"Vector: ") {};
     VectorException( string const& mess) : message( (string)"Vector: " + mess) {};
      
      virtual const char* what() const throw()
      {
        return message.c_str();
      }
      
     private:
      string const& message;
    };
    
    int main()
    {
      try
        {
          v[ 3]; //throws the exception
      catch( exception& e)
        {
          cout<< "Exception running testVector: "<< e.what()<< endl;
        }
          
      return 0;
    }
    This result in the following screen output:

    Code:
    Exception running testVector: �d��P�ͷ�l�����d��P�ͷ
    Might it be the string-member function 'c_str()' which causes the problem?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > message.c_str();
    message goes out of scope, and your c_str() pointer is dangling in mid-air with nothing to support it (perhaps).

    Does it work with true string constants which have a lifetime of the entire program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    >> string const& message;
    "message" needs to be a real object and not a reference.

    >> : message( (string)"Vector: " + mess)
    So "message" is a reference to the temp object returned by "string + string". Make "message" a real object so it can store the results of that "string + string".

    gg

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    But why didn't my compiler (g++-4.3) give me any warnings on that? Weird, normally it does.

    I modified the code:

    Code:
    class VectorException: public std::exception
    { // line 6 where error occurs
     public:
     VectorException( std::string& mess) 
       : message( (std::string)"Vector: " + mess) {};
      
      virtual const char* what() const throw()
      {
        return message.c_str();
      }
      
     private:
      std::string message;
    };
    But now the compiler generates the following message:

    Exceptions.h:6: error: looser throw specifier for ‘virtual VectorException::~VectorException()’
    /usr/include/c++/4.1.3/exception:58: error: overriding ‘virtual std::exception::~exception() throw ()’
    make: *** [Exceptions.o] Error 1
    Do I really need to override the (virtual) destructor? It didn't cause problems with the 'const&' .

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::exception and STL
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2008, 09:16 PM
  2. Deriving from std::except
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2007, 07:20 AM
  3. Deriving one class from another
    By SpudNuts in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2005, 01:15 AM
  4. Deriving Test Cases
    By test in forum C Programming
    Replies: 2
    Last Post: 10-01-2002, 05:45 PM
  5. Deriving from CWindow
    By xds4lx in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2002, 04:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21