Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88701 > unrolled thread
| Started by | Palpandi <palpandi111@gmail.com> |
|---|---|
| First post | 2015-04-09 02:31 -0700 |
| Last post | 2015-04-11 07:39 +0200 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Palpandi <palpandi111@gmail.com> |
|---|---|
| Date | 2015-04-09 02:31 -0700 |
| Subject | Exception Handling |
| Message-ID | <b6461fc8-a18b-4d61-86f9-65370b0a149d@googlegroups.com> |
Hi all, Is there any way to roll back or undo changes which are all done before exception occurs.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-09 20:36 +1000 |
| Message-ID | <mailman.161.1428575820.12925.python-list@python.org> |
| In reply to | #88701 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-04-10 01:05 +1000 |
| Message-ID | <5526951f$0$12988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #88701 |
On Thu, 9 Apr 2015 07:31 pm, Palpandi wrote: > Hi all, > > Is there any way to roll back or undo changes which are all done before > exception occurs. Not automatically. You have to program it yourself. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2015-04-10 08:11 +0200 |
| Message-ID | <mailman.195.1428646323.12925.python-list@python.org> |
| In reply to | #88701 |
Palpandi <palpandi111@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.
[toc] | [prev] | [next] | [standalone]
| From | Palpandi <palpandi111@gmail.com> |
|---|---|
| Date | 2015-04-10 01:39 -0700 |
| Message-ID | <1cd0a97d-96c7-4ee6-9cfe-c0e90b9baa55@googlegroups.com> |
| In reply to | #88701 |
Thanks for your responses. And I have one more question. This is the situation. A python application which reads data from one .xml file and generates some other file. Here what kind of exceptions occur?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-10 19:00 +1000 |
| Message-ID | <mailman.199.1428656403.12925.python-list@python.org> |
| In reply to | #88771 |
On Fri, Apr 10, 2015 at 6:39 PM, Palpandi <palpandi111@gmail.com> wrote: > Thanks for your responses. And I have one more question. > > This is the situation. A python application which reads data from one .xml file and generates some other file. Here what kind of exceptions occur? All sorts of exceptions. The best way to find out is to write your code without any try/except clauses, and then run it. When an exception happens, it'll be printed out on the console, and the program terminated. Then you can decide how your program should cope with that. Catch an exception *only* if you can actually deal with it inside your code; otherwise, just let it bubble up. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2015-04-11 07:39 +0200 |
| Message-ID | <mailman.214.1428730779.12925.python-list@python.org> |
| In reply to | #88771 |
Palpandi <palpandi111@gmail.com> writes: > Thanks for your responses. And I have one more question. > This is the situation. A python application which reads data from one .xml file and generates some other file. Here what kind of exceptions occur? Ideally, you would not get any exceptions. In the not ideal case, you may get exceptions from the XML parser (in case, your XML input file is not valid), the "os" module (for permission or filesystem related problems) and potentially exceptions (such as "AttributeError") from your own program logic (in case it would be faulty). I support Chris' recommendation to not care about exceptions intially (as they are exceptions - and do not occur frequently) and decide what to do should they really occur.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web