<p>我的python代码看起来像这样-</p>
<pre><code>try:
statement1
statement2
statement3
except Exception:
handleException
</code></pre>
<p>问题是,如果statement3生成异常,那么statement1和statement2的效果已经发生,并且我无法在<code>except</code>部分中撤消它们。有没有一个简单的方法来写这样的东西-</p>
<pre><code>check if the following block runs without exceptions:
statement1
statement2
statement3
if yes:
run entire block
if no:
handleException
</code></pre>
<p>(基本上类似于Python代码的事务原子性)。你知道吗</p>