Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88706

Re: Exception Handling

References <b6461fc8-a18b-4d61-86f9-65370b0a149d@googlegroups.com>
Date 2015-04-09 20:36 +1000
Subject Re: Exception Handling
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.161.1428575820.12925.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Apr 9, 2015 at 7:31 PM, Palpandi <palpandi111@gmail.com> wrote:
> Hi all,
>
> Is there any way to roll back or undo changes which are all done before exception occurs.

In Python itself? Not directly; there are no facilities for undoing
Python code. But if you're serious about integrity, you probably want
to be (or already are) using a database, eg PostgreSQL, and storing
data in there. In that case, you can follow a fairly straight-forward
model, and one that some database connection modules even make easy
for you:

with dbconnection:
    # do stuff that might affect the database
    # and might throw an exception

As you leave the 'with' block, Python will tell the database
connection "hey, we got out without a problem", or "hey, we're leaving
here because of an exception". In the first case, the database will
commit, so everything you've done really happens. In the second, it'll
roll back, which will undo everything perfectly.

But this applies only to the database. For this to work, you have to
organize your code around that; make sure that everything that matters
is in the database, and local objects can't ever need to be "undone".

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Exception Handling Palpandi <palpandi111@gmail.com> - 2015-04-09 02:31 -0700
  Re: Exception Handling Chris Angelico <rosuav@gmail.com> - 2015-04-09 20:36 +1000
  Re: Exception Handling Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-10 01:05 +1000
  Re: Exception Handling dieter <dieter@handshake.de> - 2015-04-10 08:11 +0200
  Re: Exception Handling Palpandi <palpandi111@gmail.com> - 2015-04-10 01:39 -0700
    Re: Exception Handling Chris Angelico <rosuav@gmail.com> - 2015-04-10 19:00 +1000
    Re: Exception Handling dieter <dieter@handshake.de> - 2015-04-11 07:39 +0200

csiph-web