Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-03-27 08:38:38


Klaus Nowikow wrote:
>Reece Dunn wrote:
>[snip]
> > A trick I use when handling HRESULT error codes is to have a
> > class like this
> > (adapted for int error type):
> >
> > class errorcheck
> > {
>[snip]
> > public:
> > inline errorcheck( int ec ): error( ec )
> > {
> > if( ec < 0 ) throw( *this );
> > }
>
>Careful. Throwing an exception from a constructor
>means that the object won't be constructed (i. e., does not exist).
>So you are throwing a non-existing object here.
>
>Or am I wrong?

If I've got the standard correct, the object will be partially constructed
up to the point where the exception is thrown. Thus, if this throws an
exception, the error member will be set. If you had:

struct test
{
   errorcheck ec;
   std::string msg;

   test(): ec( -1 ), msg( "hello" )
   {
   }
};

an exception will be generated before the msg string is set and thus test
would be invalid. However, I use it like:

class myfile
{
   ...
   myfile( const char * fn )
   {
      errorcheck ec = open( fn, ... );
   }
   ~myfile()
   {
      close( ... );
   }
};

where because the file is not opened if an exception is thrown above, it
will not call the destructor to close a non-opened file.

Regards,
Reece

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk