Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested try...except

38 views
Skip to first unread message

Magnus....@gmail.com

unread,
Apr 2, 2008, 9:06:47 AM4/2/08
to
Hi,

I found the following code on the net -

http://mail-archives.apache.org/mod_mbox/httpd-python-cvs/200509.mbox/%3C20050924104...@minotaur.apache.org%3E

def count(self):
- db = sqlite.connect(self.filename,
isolation_level=ISOLATION_LEVEL)
- try:
- try:
- cur = db.cursor()
- cur.execute("select count(*) from sessions")
- return cur.fetchone()[0]
- finally:
- cur.close()
- finally:
- db.close()

I don't understand though why the second try is not after the line cur
= db.cursor(). Can anyone explain for me why?

/Barry.

cokof...@gmail.com

unread,
Apr 2, 2008, 9:12:05 AM4/2/08
to
On Apr 2, 3:06 pm, Magnus.Morab...@gmail.com wrote:
> Hi,
>
> I found the following code on the net -
>
> http://mail-archives.apache.org/mod_mbox/httpd-python-cvs/200509.mbox/%3C20050924104732.5116.qm...@minotaur.apache.org%3E

>
> def count(self):
> - db = sqlite.connect(self.filename,
> isolation_level=ISOLATION_LEVEL)
> - try:
> - try:
> - cur = db.cursor()
> - cur.execute("select count(*) from sessions")
> - return cur.fetchone()[0]
> - finally:
> - cur.close()
> - finally:
> - db.close()
>
> I don't understand though why the second try is not after the line cur
> = db.cursor(). Can anyone explain for me why?
>
> /Barry.

Better question is why is there a try with no except...

Better yet, WHY is there two TRY statements when there could quite
happily be only one...

Towards what you are asking, I GUESS...because the author hoped to
handle the cases where cur failed to get assigned...but then
his .close method of it would likely not work anyway...I mean...does
this even work...YUCK

Duncan Booth

unread,
Apr 2, 2008, 9:12:35 AM4/2/08
to
Magnus....@gmail.com wrote:

Simple: because not all code found on the net is correct.

Magnus....@gmail.com

unread,
Apr 2, 2008, 9:15:33 AM4/2/08
to

I shouldn't have written "Nested try...except" as the title, instead I
mean "Nested try...finally". Sorry about that...

Anyway, how would you do this? That is, use a finally to close the
network connection and the cursor?

Thanks for your help,

Barry

Magnus....@gmail.com

unread,
Apr 2, 2008, 9:22:50 AM4/2/08
to

Here's what I would do. Is it OK?

def ExecuteWithNoFetching(self, queryString):

sqlServerConnection = adodbapi.connect (";".join (connectors))
try:
cursor = sqlServerConnection.cursor()
try:
cursor.execute(queryString)
raise Exception("Exception")
sqlServerConnection.commit()
finally:
cursor.close()
finally:
sqlServerConnection.close()

Nanjundi

unread,
Apr 2, 2008, 12:25:21 PM4/2/08
to

No.. Why do you have raise statement?
"sqlServerConnection.commit()" never gets executed.
-N

bruno.des...@gmail.com

unread,
Apr 2, 2008, 7:00:54 PM4/2/08
to

It's demonstration code.

> -N

bruno.des...@gmail.com

unread,
Apr 2, 2008, 7:03:57 PM4/2/08
to
On 2 avr, 15:12, cokofree...@gmail.com wrote:
> On Apr 2, 3:06 pm, Magnus.Morab...@gmail.com wrote:
>
>
>
> > Hi,
>
> > I found the following code on the net -
>
> > http://mail-archives.apache.org/mod_mbox/httpd-python-cvs/200509.mbox/%3C20050924104732.5116.qm...@minotaur.apache.org%3E
>
> > def count(self):
> > - db = sqlite.connect(self.filename,
> > isolation_level=ISOLATION_LEVEL)
> > - try:
> > - try:
> > - cur = db.cursor()
> > - cur.execute("select count(*) from sessions")
> > - return cur.fetchone()[0]
> > - finally:
> > - cur.close()
> > - finally:
> > - db.close()
>
> > I don't understand though why the second try is not after the line cur
> > = db.cursor(). Can anyone explain for me why?
>
> > /Barry.
>
> Better question is why is there a try with no except...

Because the author doesn't want to handle the exception here, only
make sure resources are freed.

> Better yet, WHY is there two TRY statements when there could quite
> happily be only one...
>
> Towards what you are asking, I GUESS...because the author hoped to
> handle the cases where cur failed to get assigned...but then
> his .close method of it would likely not work anyway...I mean...does
> this even work...YUCK

Indeed.

bruno.des...@gmail.com

unread,
Apr 2, 2008, 7:22:16 PM4/2/08
to

slightly OT : where this 'connectors' comes from ?

> try:
> cursor = sqlServerConnection.cursor()
> try:
> cursor.execute(queryString)
> raise Exception("Exception")
> sqlServerConnection.commit()
> finally:
> cursor.close()
> finally:
> sqlServerConnection.close()

Else it looks ok, even if a bit paranoïd. Also, opening a db
connection for each and every single query might not be the best
solution... And finally (no pun intented), this won't let you chain
calls within a single transaction.

Another, a bit more flexible solution could be something like:

def run_in_transaction(*funcs):


sqlServerConnection = adodbapi.connect (";".join (connectors))
try:
cursor = sqlServerConnection.cursor()
try:

for func in funcs:
func(cursor)
except Exception, e:
sqlServerConnection.rollback()
raise
else:


sqlServerConnection.commit()
finally:
cursor.close()
finally:
sqlServerConnection.close()

def do_something(cursor):
cursor.execute("some update query")

def do_something_else(cursor):
cursor.execute("some select query")
for row in cursor.fetchall():
if some_condition(row):
cursor.execute("some update query")

run_in_transaction(do_something, do_something_else)

And also, there's this new 'with' statement...

My 2 cents...

Carl Banks

unread,
Apr 2, 2008, 8:08:32 PM4/2/08
to
On Apr 2, 9:06 am, Magnus.Morab...@gmail.com wrote:
> Hi,
>
> I found the following code on the net -
>
> http://mail-archives.apache.org/mod_mbox/httpd-python-cvs/200509.mbox/%3C20050924104732.5116.qm...@minotaur.apache.org%3E

>
> def count(self):
> - db = sqlite.connect(self.filename,
> isolation_level=ISOLATION_LEVEL)
> - try:
> - try:
> - cur = db.cursor()
> - cur.execute("select count(*) from sessions")
> - return cur.fetchone()[0]
> - finally:
> - cur.close()
> - finally:
> - db.close()
>
> I don't understand though why the second try is not after the line cur
> = db.cursor(). Can anyone explain for me why?

It's a pretty common mistake to make, I assume because there's a
tendency to line up the init and finalize statements. In other words,
it looks wrong for open and close to be in different columns:

open()
try:
do_stuff()
finally:
close()

It's almost always wrong for initiazation to be inside of the try
block.

Perhaps the advent of with blocks will help reduce this error in the
future.


Carl Banks

Duncan Booth

unread,
Apr 3, 2008, 10:36:55 AM4/3/08
to
Carl Banks <pavlove...@gmail.com> wrote:

> Perhaps the advent of with blocks will help reduce this error in the
> future.

Indeed, and to encourage its use I think this thread ought to include the
'with statement' form of the function:

from __future__ import with_statement
from contextlib import closing

def count(self):
with closing(sqlite.connect(
self.filename,isolation_level=ISOLATION_LEVEL)) as db:
with closing(db.cursor()) as cur:


cur.execute("select count(*) from sessions")

return cur.fetchone()[0]

0 new messages