Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is differece between exception safe and exception neutral?

Reply
Thread Tools

what is differece between exception safe and exception neutral?

 
 
siddhu
Guest
Posts: n/a
 
      05-17-2011
I think I understand the term "Exception neutral" which means
exception should get propagated to the caller without loosing the
integrity of the program. I did not understand the term "Exception
safe" correctly. Does it mean that the code which will be called
exception safe should not throw any exception and maintain the
integrity of the program? How does it differ from "Exception neutral"?
Your input would be of great help.
P.s. Please do not mind my English

Thanks,
Siddhu
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      05-17-2011
siddhu <(E-Mail Removed)> wrote:
> I think I understand the term "Exception neutral" which means
> exception should get propagated to the caller without loosing the
> integrity of the program. I did not understand the term "Exception
> safe" correctly. Does it mean that the code which will be called
> exception safe should not throw any exception and maintain the
> integrity of the program? How does it differ from "Exception neutral"?
> Your input would be of great help.


AFAIK exception safety does not mean the code doesn't throw exceptions.
It means that if an exception is thrown anywhere, it will not leak
anything.

For example, this is not exception-safe:

void foo()
{
int* array = new int[amount];
do_something_with(array);
delete[] array;
}

This is:

void foo()
{
std::vector<int> array(amount);
do_something_with(array);
}
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      05-18-2011
On 17 mai, 18:16, Juha Nieminen <(E-Mail Removed)> wrote:
> siddhu <(E-Mail Removed)> wrote:
> > I think I understand the term "Exception neutral" which means
> > exception should get propagated to the caller without loosing the
> > integrity of the program. I did not understand the term "Exception
> > safe" correctly. Does it mean that the code which will be called
> > exception safe should not throw any exception and maintain the
> > integrity of the program? How does it differ from "Exception neutral"?
> > Your input would be of great help.

>
> * AFAIK exception safety does not mean the code doesn't throw exceptions.
> It means that if an exception is thrown anywhere, it will not leak
> anything.
>
> * For example, this is not exception-safe:
>
> void foo()
> {
> * * int* array = new int[amount];
> * * do_something_with(array);
> * * delete[] array;
>
> }


Well, if do_something_with() has a nothrow guarantee, this is
exception safe.

>
> * This is:
>
> void foo()
> {
> * * std::vector<int> array(amount);
> * * do_something_with(array);
> }


AFAIK exception neutral only mean that upon exception, if the state of
the program was modified, the state of the program remains valid when
going out of the scope.

In practice, any resource acquired should be released (memory, mutex,
file, ...) and there shouldn't be data in inconsistent state
(typically dangling pointers).

IIRC exception safe means that the former state of the system is
preserved.

The usual example is the copy operator of a class storing dynamically
allocated data:

This is unsafe:

Foo& operator=(Foo const & foo)
{
if( myData != foo.myData )
{
delete myData;
myData = new MyData(foo.myData); // if throw here, this->myData is
dangling pointer
}
return *this;
}

This is exception neutral (assuming null myData is a valid state):
Foo& operator=(Foo const & foo)
{
if( myData != foo.myData )
{
delete myData;
myData = NULL;
myData = new MyData(foo.myData); // if throw here, this->myData is
lost
}
return *this;
}

This is exception safe:
Foo& operator=(Foo const & foo)
{
if( myData != foo.myData )
{
MyData* newData = new MyData(foo.myData);
std::swap(newData, myData); // this is nothrow
delete newData;
}
return *this;
}

--
Michael
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      05-18-2011

"siddhu" <(E-Mail Removed)>

>I think I understand the term "Exception neutral" which means
> exception should get propagated to the caller without loosing the
> integrity of the program. I did not understand the term "Exception
> safe" correctly. Does it mean that the code which will be called
> exception safe should not throw any exception and maintain the
> integrity of the program? How does it differ from "Exception neutral"?


exception safe means it brngs at least the basic Abrahams guarantee
http://en.wikipedia.org/wiki/Exception_guarantees . You ought to specify
actually which guarantee your stuff provides along with other clues...

Neutral (I normally use the term "transparent" instead) means it does not
mess with exceptions: it is safe, plus in its body will not throw a specific
exception, neither catch one without throw it forward.

Preferably it has no try/catch/throw in the body at all and uses RAII tech
to handle the state.
Formally it could also do a try{}catch(...){ some cleanup; throw;} but that
form should have trouble on a review. But some trace/logging stuff may
require a similar layout.

The point is the caller will get exactly the exceptions thrown from below in
their original form.



 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      05-18-2011
On 18 Mai, Michael Doubez wrote:
> This is unsafe:
>
> Foo& operator=(Foo const & foo)
> {
> * if( myData != foo.myData )
> * {
> * * delete myData;
> * * myData = new MyData(foo.myData); // if throw here, this->myDatais
> dangling pointer
> * }
> * return *this;
>
> }
>
> This is exception neutral (assuming null myData is a valid state):
> Foo& operator=(Foo const & foo)
> {
> * if( myData != foo.myData )
> * {
> * * delete myData;
> * * myData = NULL;
> * * myData = new MyData(foo.myData); // if throw here, this->myDatais
> lost
> * }
> * return *this;
>
> }
>
> This is exception safe:
> Foo& operator=(Foo const & foo)
> {
> * if( myData != foo.myData )
> * {
> * * MyData* newData = new MyData(foo.myData);
> * * std::swap(newData, myData); *// this is nothrow
> * * delete newData;
> * }
> * return *this;
>
> }
>
> --
> Michael


Very nicely put. Do you mind if we use this in other threads (properly
cited, of course)?

Stuart
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      05-18-2011
On 18 mai, 17:03, Stuart Redmann <(E-Mail Removed)> wrote:
> On 18 Mai, Michael Doubez wrote:
>
>
>
>
>
>
>
>
>
> > This is unsafe:

>
> > Foo& operator=(Foo const & foo)
> > {
> > * if( myData != foo.myData )
> > * {
> > * * delete myData;
> > * * myData = new MyData(foo.myData); // if throw here, this->myData is
> > dangling pointer
> > * }
> > * return *this;

>
> > }

>
> > This is exception neutral (assuming null myData is a valid state):
> > Foo& operator=(Foo const & foo)
> > {
> > * if( myData != foo.myData )
> > * {
> > * * delete myData;
> > * * myData = NULL;
> > * * myData = new MyData(foo.myData); // if throw here, this->myData is
> > lost
> > * }
> > * return *this;

>
> > }

>
> > This is exception safe:
> > Foo& operator=(Foo const & foo)
> > {
> > * if( myData != foo.myData )
> > * {
> > * * MyData* newData = new MyData(foo.myData);
> > * * std::swap(newData, myData); *// this is nothrow
> > * * delete newData;
> > * }
> > * return *this;

>
> > }

>
> Very nicely put. Do you mind if we use this in other threads (properly
> cited, of course)?


None, just remove my mistake: this is exception unsafe/safe basic/safe
strong.

Exception neutral has another meaning in C++ (cf Balog Pal answer).

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception safe & Exception Neutral deep_copy smart pointer Nindi73@yahoo.co.uk C++ 3 11-10-2006 10:55 AM
who can tell me the differece of this two ? usr.root@gmail.com C Programming 18 09-23-2005 08:00 AM
Safe Mode (?) - It is meant to be normal mode but looks like safe mode English Patient Computer Support 3 10-03-2004 11:10 PM
Re: Those cute little "WORK-SAFE" / "NOT WORK-SAFE" tags that people put in the Subject headers of their posts... Soapy Digital Photography 1 08-16-2004 12:07 PM
$SAFE = 5 and Safe Ruby Misleading? kirindave@lensmen.net Ruby 1 08-11-2003 11:35 PM



Advertisments