Re: String corrupted after throw

From: Alf P. Steinbach (alfps_at_start.no)
Date: 03/08/04


Date: Mon, 08 Mar 2004 08:54:23 GMT


* "William Payne" <mikas493_no_s_p_a_m_@student.liu.se> schriebt:
>

Not a relation of Major Payne, are you? ;-) ... Joking. Couldn't resist.

 
> Thanks for your reply. I was throwing a string literal at first, but I
> didn't realise it does not have automatic storage but instead exists
> throughout the life-time of the program (correct?). Then I changed to what I
> had in my first post because I wanted to include the value (the int) causing
> the exception to be included in the error message. Now, as you, suggested, I
> am throwing an object of a class derived from std::exception and catching
> const references to it. The class looks like this:
>
> class BinaryTreeError : public std::exception

  class BinaryTreeError: public std::runtime_error

> {
> public:
> explicit BinaryTreeError(const std::string& error_message)
> :
> m_error_message(error_message)

        :
        std::runtime_error( error_message )

> {
> ; /* Do nothing. */
> }
>
> virtual ~BinaryTreeError() throw()
> {
> ; /* Do nothing. */
> }
>
> const char* what() const throw()
> {
> return m_error_message.c_str();
> }

     -- remove 'what' function.

>
> private:
> std::string m_error_message;

  -- remove 'm_error_message'.

> };
>
> Everything seems to work now! However, I left the call to sprintf there, the
> integer would have to contain a large value indeed for that string to
> overflow.

That's OK, but beware of floating point values.

Now, there are _three_ main reasons for deriving from std::runtime_error
instead of directly from std::exception:

1) std::runtime_error keeps track of an error message, and since it's a
   standard library class it might do so more efficiently than you can.

2) std::runtime_error is the base class for ordinary runtime errors,
   as opposed to for example std::logic_error.

3) In many situations it's necessary to distinguish between "hard" and
   "soft" exceptions, where the former are meant to be caught only at the
   highest level of the program, leading to eventual termination. So at
   lower levels, catch only std::runtime_error (if anything). And throw
   only std::runtime_error, except if you want the program to abort. Other
   exceptions like std::bad_alloc and std::bad_cast will then have "hard"
   effect automatically. One simple convention that spares you much work.

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Relevant Pages

  • Re: comments on this Binary Searh Tree template?
    ... > POSITION GetRootconst; ... This code is not exception safe, but I guess you aren't too worried about ... > if (!pNode) ... This code has two points, it does the correct thing on memory exhaustion, ...
    (comp.lang.cpp)
  • RE: Biztalk 2006 Tutorial 3
    ... There are some known issues w/ the tutorials. ... Below is the error message that appears in the event ... Exception type: ServiceCreationException ... md, Objectargs, Object server, Int32 methodPtr, Boolean fExecuteInContext, ...
    (microsoft.public.biztalk.general)
  • RE: Concerns about exception string revealing internals/data about
    ... exceptions could return certain information as part of the error message. ... exception occurs in my program, is it possible that the exception error ... built in ADO objects in Visual Studio for Microsoft SQL Server. ... the build-in controls in SQL Server2000 or .Net winform/Asp.net controls? ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Concerns about exception string revealing internals/data about
    ... Sorry in last post I meant an "unhandled" exception should never be shown to ... exceptions could return certain information as part of the error message. ... built in ADO objects in Visual Studio for Microsoft SQL Server. ... the build-in controls in SQL Server2000 or .Net winform/Asp.net controls? ...
    (microsoft.public.dotnet.framework.clr)
  • RE: Biztalk 2006 Tutorial 3
    ... Below is the error message that appears in the event ... Exception type: ServiceCreationException ... Microsoft.BizTalk.XLANGs.BTXEngine.BTXSession._createOrRehydrateRealService(Guid& instanceId, IBTMessage currMsg) ... md, Objectargs, Object server, Int32 methodPtr, Boolean fExecuteInContext, ...
    (microsoft.public.biztalk.general)