what() method of exceptions derived form std::exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boogie El Aceitoso

    what() method of exceptions derived form std::exception

    Hi,

    I have an exception class for winapi errors (returns a formated error mesage,
    usign ::GetLastError( ) et al, with what()).

    It looks like this:

    class WinapiException : public std::exception
    {
    public:
    WinapiException (const std::string & extraInfo);
    virtual const char* what() const throw();
    private:
    std::string _extraInfo;
    };

    WinapiException ::WinapiExcepti on(const std::string & extraInfo)
    :_extraInfo(ext raInfo)
    {
    // Nothing
    }

    The problem is the what() method. Mine looks like this:

    const char* WinapiException ::what() const throw()
    {
    std::string rc;

    rc = typeid(this).na me();

    // errorString formats the winapi error message and returns an std::string
    rc += "GetLastErr or reports:\n" + errorString(::G etLastError());

    if( _extraInfo.size () > 0)
    {
    rc += "\nExtra Info:\n" + _extraInfo;
    }
    else
    {
    rc += "\nNo extra info available.";
    }

    return rc.c_str(); // here's the problem!!!
    }

    The what() returns a const char* that points to something that has already
    been destroyed and freed.

    What's the best solution for this? O:-)
  • Robert W Hand

    #2
    Re: what() method of exceptions derived form std::exception

    On Tue, 04 Nov 2003 11:57:22 +0100, Boogie El Aceitoso
    <frr149@telefon ica.net> wrote:
    [color=blue]
    >
    > return rc.c_str(); // here's the problem!!!
    > }
    >
    >The what() returns a const char* that points to something that has already
    >been destroyed and freed.
    >
    >What's the best solution for this? O:-)[/color]

    I am not sure that you have given us enough information to confidently
    state a best solution. I can see several ways out.

    1. You could make rc a non-static data member of your class.
    2. You could declare rc static in the member function.
    3. You could declare rc a static data member of the class.
    4. You could allocate a dynamic array for the array. But that is
    almost always more trouble than its worth. Who deletes the array?

    Anyway, there are four ways of handling the problem. I might make it
    a data member (static or non-static).
    --

    Best wishes,

    Bob

    Comment

    • Ron Natalie

      #3
      Re: what() method of exceptions derived form std::exception


      "Boogie El Aceitoso" <frr149@telefon ica.net> wrote in message news:7f1fqvk7s7 ti474ooogop3a30 4cr9e5bs9@4ax.c om...
      [color=blue]
      >
      > The what() returns a const char* that points to something that has already
      > been destroyed and freed.
      >[/color]
      The easiest way is to keep the data in a string which is a member of the exception
      object.

      There are two ways you could do this:

      1. You could just add another member "what_strin g" and fill it in rather than "rc"
      in your what() function (you could even notice if this had been set and avoid recomputing
      the string on subsuequent calls to what()).

      2. The WinapiException could do what you now do in the what() function and store only the
      "whatString " rather than the extrainfo in the object.


      Comment

      • Stephen M. Webb

        #4
        Re: what() method of exceptions derived form std::exception

        Boogie El Aceitoso <frr149@telefon ica.net> wrote in message news:<7f1fqvk7s 7ti474ooogop3a3 04cr9e5bs9@4ax. com>...[color=blue]
        >
        > I have an exception class for winapi errors (returns a formated error mesage,
        > usign ::GetLastError( ) et al, with what()).
        >
        > It looks like this:
        >
        > class WinapiException : public std::exception
        > {
        > public:
        > WinapiException (const std::string & extraInfo);
        > virtual const char* what() const throw();
        > private:
        > std::string _extraInfo;
        > };
        >
        > WinapiException ::WinapiExcepti on(const std::string & extraInfo)
        > :_extraInfo(ext raInfo)
        > {
        > // Nothing
        > }
        >
        > The problem is the what() method. Mine looks like this:
        >
        > const char* WinapiException ::what() const throw()
        > {
        > std::string rc;
        >
        > rc = typeid(this).na me();
        >
        > // errorString formats the winapi error message and returns an std::string
        > rc += "GetLastErr or reports:\n" + errorString(::G etLastError());
        >
        > if( _extraInfo.size () > 0)
        > {
        > rc += "\nExtra Info:\n" + _extraInfo;
        > }
        > else
        > {
        > rc += "\nNo extra info available.";
        > }
        >
        > return rc.c_str(); // here's the problem!!!
        > }
        >
        > The what() returns a const char* that points to something that has already
        > been destroyed and freed.[/color]

        The answer is simple: have what() return something that isn't already
        destroyed and freed.

        WinapiException ::WinapiExcepti on(const std::string & extraInfo)
        :_extraInfo(typ eid(this).name( ))
        {
        // errorString formats the winapi error message and returns an
        std::string
        _extraInfo += "GetLastErr or reports:\n" +
        errorString(::G etLastError());

        if (extraInfo.size () > 0)
        {
        _extraInfo += "\nExtra Info:\n" + extraInfo;
        }
        else
        {
        _extraInfo += "\nNo extra info available.";
        }
        }

        const char* WinapiException ::what() const throw()
        {
        return _extraInfo.c_st r();
        }

        This has the added advantage that ::GetLastError( ) will be called at
        the point when the exception is thrown, rather than later when the
        exception is reported and its value may have been affected by
        intervening system calls.

        --
        Stephen M. Webb

        Comment

        • Andrey Tarasevich

          #5
          Re: what() method of exceptions derived form std::exception

          Boogie El Aceitoso wrote:[color=blue]
          > ...
          > I have an exception class for winapi errors (returns a formated error mesage,
          > usign ::GetLastError( ) et al, with what()).
          >
          > It looks like this:
          >
          > class WinapiException : public std::exception
          > {
          > public:
          > WinapiException (const std::string & extraInfo);
          > virtual const char* what() const throw();
          > private:
          > std::string _extraInfo;
          > };
          >
          > WinapiException ::WinapiExcepti on(const std::string & extraInfo)
          > :_extraInfo(ext raInfo)
          > {
          > // Nothing
          > }
          > ...[/color]

          Just to add something less relevant to other people's replies:

          The above definition of 'WinapiExceptio n' class is ill-formed. Your
          class 'WinapiExceptio n' contains a subobject of type 'std::string'. The
          'std::string's destructor has no exception specification (allows all
          types of exceptions), which immediately means that the implicitly
          declared destructor of class 'WinapiExceptio n' will also allow all types
          of exceptions (see 15.4/13). At the same time the virtual destructor of
          'std::exception ' is declared with 'throw()' exception specification (no
          exceptions allowed). According to 15.4/3, the above definition of
          'WinapiExceptio n' is ill-formed, since the declaration of virtual
          function 'WinapiExceptio n::~WinapiExcep tion' is less restrictive than
          the exception specification of base class' virtual function it overrides
          ('std::exceptio n::~exception') . (See also an example in 15.4/13, which
          is similar to yours in this respect.)

          It is very likely that your compiler accepts the above code. But don't
          be surprised if one day it will start complaining.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          Working...