what is differece between exception safe and exception neutral?

Discussion in 'C++' started by siddhu, May 17, 2011.

  1. siddhu

    siddhu Guest

    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
    siddhu, May 17, 2011
    #1
    1. Advertising

  2. siddhu <> 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);
    }
    Juha Nieminen, May 17, 2011
    #2
    1. Advertising

  3. On 17 mai, 18:16, Juha Nieminen <> wrote:
    > siddhu <> 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
    Michael Doubez, May 18, 2011
    #3
  4. siddhu

    Balog Pal Guest

    "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"?


    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.
    Balog Pal, May 18, 2011
    #4
  5. 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
    Stuart Redmann, May 18, 2011
    #5
  6. On 18 mai, 17:03, Stuart Redmann <> 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
    Michael Doubez, May 18, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hannes Schmiderer

    Localization: neutral language out of main assembly

    Hannes Schmiderer, Aug 19, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    1,523
    Hannes Schmiderer
    Aug 21, 2003
  2. Keith
    Replies:
    1
    Views:
    578
    Brzezi
    Jun 13, 2005
  3. Replies:
    18
    Views:
    450
    Alexei A. Frounze
    Sep 23, 2005
  4. Replies:
    3
    Views:
    1,152
  5. Richard

    domain-neutral loading .net 1.x and iis

    Richard, Feb 20, 2006, in forum: ASP General
    Replies:
    0
    Views:
    95
    Richard
    Feb 20, 2006
Loading...

Share This Page