title: Object-oriented programming & transactions
body: <p>A little intro:</p>
  
  <p>Class contains fields and methods (let me skip properties this time).<br>
  Fields represent a <em>state</em> of the class.<br>
  Methods describe <em>behavior</em> of the class.</p>
  
  <p>In a well-designed class, a method won't change the class's state if it throws an exception, right? (In other words, whatever happens, class's state shouldn't be corrupted)</p>
  
  <p>Question:</p>
  
  <p>Is there a framework, a design pattern, best practice or a programming language to call a sequence of methods in a transactional style, so that either class's state don't get changed (in case of exception), or everything succeeds?</p>
  
  <p>E.g.:</p>
  
  <pre><code>// the class foo is now in the state S1
  foo.MoveToState2();
  // it is now (supposed to be) in the state S2
  foo.MoveToFinalState();
  // it is now (supposed to be) in the state, namely, S3
  </code></pre>
  
  <p>Surely, an exception might occur both in <code>MoveToState2()</code> and <code>MoveToFinalState()</code>. But from this block of code I want the class <code>foo</code> to be either in the state S1 or S3.</p>
  
  <p>This is a simple scenario with a single class involved, no <code>if</code>'s, no <code>while</code>'s, no side effects, but I hope the idea is clear.</p>