Exception Handling

dieter dieter at handshake.de
Fri Apr 10 08:11:51 CEST 2015

Palpandi <palpandi111 at gmail.com> writes:

> Is there any way to roll back or undo changes which are all done before exception occurs.

You are mostly interested in such a function when you handle
persistent data (data which persists across program activations -
otherwise, you can always quit the current program and restart it again).

Python usually does not handle persistent data by itself but
uses some kind of library or external system - e.g. a
relational database. Those external components may support
the concept of "transaction". Their primary function
is to ensure a consistent persistant state in view of concurrent
modifications. As a side effect, they support a "rollback"
which rolls back all modifications to persistent data made by the
rolled back transaction.

With some limitations, you can use the Python modules
"ZODB3" and "transaction" (available on "PyPI") to use
transactions in your program - even if you are not interested
in persistent data. Its "rollback" will not undo all changes but only
changes to "ZODB object"s.

More information about the Python-list mailing list