Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strong exception guarantee VS basic exception guarantee

By Abrahams we have 3 types of exception :

  1. Nothrow
  2. Basic exception guarantee
  3. Strong exception guarantee

Basic means (please correct me if I'm wrong) that Invariants are preserved e.g that the invariants of the component are preserved, and no resources are leaked , where Strong that the operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started .

  1. What does it mean that Invariants are preserved ? that if I have a valid value in one of my variables then it wouldn't (take for example a pointer) hold a NULL ?

  2. Referring to Strong exception guarantee , does it mean that all my variables would store the exact same values before the exception was thrown ?

for example :

int main()

{
    int j = 1;
    int *p = &j;

    // do some stuff
    j = 2;
    throw 1;

}

Then after I throw , j would hold the value 2 or 1 ?

Regards

like image 253
JAN Avatar asked Aug 27 '12 07:08

JAN


People also ask

What is strong exception guarantee?

Strong exception guarantee -- If the function throws an exception, the state of the program is rolled back to the state just before the function call. (for example, std::vector::push_back) Basic exception guarantee -- If the function throws an exception, the program is in a valid state.

What is basic guarantee?

The basic guarantee states that if an exception occurs, no memory is leaked and the object is still in a usable state even though the data might have been modified.

How many levels are there in exception safety?

The four levels. Any piece of code we write has one of four levels of exception safety: No guarantee, the basic guarantee, the strong guarantee anf the nothrow guarantee.

What does the exception safe programming mean?

Exception safe programming is programming so that if any piece of code that might throw an exception does throw an exception, then the state of the program is not corrupted and resources are not leaked. Getting this right using traditional methods often results in complex, unappealing and brittle code.


2 Answers

  1. Basic guarantee: after an exception was thrown, objects are left in a consistent, usable state. No resources are leaked and invariants are preserved. The state of an object might have changed, but it is still usable. For example, a date object whose day value has become -1 is not usable anymore, as its invariants say that day is in range [1, 31].

  2. Strong guarantee (additional to 1): a date object has value 2012-12-31. After an operation that tries to modify that value has failed, the value of that object is still 2012-12-31. Maybe some internal state has changed, but the logical state from the client point of view is unchanged.

like image 89
hansmaad Avatar answered Nov 11 '22 16:11

hansmaad


In your case, there is NO exception guarantee. (This is basically case 0). The Wikipedia article you quoted is clear: "The rules apply to class implementations". Furthermore, after you throw, the variable j is out of scope and no longer exists. You can't even talk about its address anymore, let alone the value

Usually, class invariants are defined by the class author, so it means whatever the class author means. I don't understand your point 1. NULL is a valid value for a pointer.

Your second point is a good one. The definition isn't absolute. For instance, an operation on a string data member may increase its capacity. You can observe this on the outside via a const&. Yet, that string capacity usually is not considered as part of the string value, and therefore not as part of the class invariant.

like image 24
MSalters Avatar answered Nov 11 '22 16:11

MSalters