473,383 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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::WinapiException(const std::string & extraInfo)
:_extraInfo(extraInfo)
{
// Nothing
}

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

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

rc = typeid(this).name();

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

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:-)
Jul 19 '05 #1
4 8063
On Tue, 04 Nov 2003 11:57:22 +0100, Boogie El Aceitoso
<fr****@telefonica.net> wrote:

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:-)


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
Jul 19 '05 #2

"Boogie El Aceitoso" <fr****@telefonica.net> wrote in message news:7f********************************@4ax.com...

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

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_string" 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.
Jul 19 '05 #3
Boogie El Aceitoso <fr****@telefonica.net> wrote in message news:<7f********************************@4ax.com>. ..

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::WinapiException(const std::string & extraInfo)
:_extraInfo(extraInfo)
{
// Nothing
}

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

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

rc = typeid(this).name();

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

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.


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

WinapiException::WinapiException(const std::string & extraInfo)
:_extraInfo(typeid(this).name())
{
// errorString formats the winapi error message and returns an
std::string
_extraInfo += "GetLastError reports:\n" +
errorString(::GetLastError());

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

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

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
Jul 19 '05 #4
Boogie El Aceitoso wrote:
...
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::WinapiException(const std::string & extraInfo)
:_extraInfo(extraInfo)
{
// Nothing
}
...


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

The above definition of 'WinapiException' class is ill-formed. Your
class 'WinapiException' 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 'WinapiException' 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
'WinapiException' is ill-formed, since the declaration of virtual
function 'WinapiException::~WinapiException' is less restrictive than
the exception specification of base class' virtual function it overrides
('std::exception::~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

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Miller | last post by:
Is there any particular good reason to inherit from exceptions.Exception? I've never seen any code that depends on what the base class(es) of a raised exception is (are). My use case is in a...
2
by: Maurice LING | last post by:
Hi, I'm trapping exceptions with these codes... try: except IOError: except TypeError: except:
3
by: Noah Roberts | last post by:
I am having some problems with deriving the std::exception class. My compiler (g++-2.95) works with it just fine, but it does in a lot of broken cases. I have a user/developer that can't compile...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
1
by: vlar | last post by:
01/30/2004 I have posted "Serious bug in ngen.exe" in this thread but so far (in .NET 1.1 SP1) this bug is not yet fixed !!! So I am repeating this post again: Ngen stops catching of derived...
5
by: mthgk | last post by:
I have a C# MDI app. The child forms do alot of work, so this work is perfomed on a different thread created using ThreadPool.QueueUserWorkItem(). Because the status of the work is important to...
4
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I...
2
by: Lance.Jellyfish | last post by:
Hi, right now, something is very strange... The .net exception handler is disabled!! When I throw an exception or if I try to use an object that is not set ( = null) I do not receive any...
6
by: PGP | last post by:
Where will i look to find potential exceptions thrown by a method? I am looking at Object Browser and couldnt find any, MSDN doesnt seem to provide a consistent format for this either. Any advice...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.