what is differece between exception safe and exception neutral?

S

siddhu

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
 
J

Juha Nieminen

siddhu said:
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);
}
 
M

Michael Doubez

siddhu said:
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;
}
 
B

Balog Pal

siddhu said:
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.
 
S

Stuart Redmann

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;

}

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

Stuart
 
M

Michael Doubez

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top