Nested try...except

  • Thread starter Magnus.Moraberg
  • Start date
M

Magnus.Moraberg

Hi,

I found the following code on the net -

http://mail-archives.apache.org/[email protected]>

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.
 
C

cokofreedom

Hi,

I found the following code on the net -

http://mail-archives.apache.org/[email protected]>

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
 
M

Magnus.Moraberg

I found the following code on the net -

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?

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

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
 
M

Magnus.Moraberg

On Apr 2, 3:06 pm, (e-mail address removed) wrote:
Hi,
I found the following code on the net -
http://mail-archives.apache.org/[email protected]>
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

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

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()
 
N

Nanjundi

On 2 Apr, 15:12, (e-mail address removed) wrote:
On Apr 2, 3:06 pm, (e-mail address removed) wrote:
Hi,
I found the following code on the net -
http://mail-archives.apache.org/[email protected]>
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
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,

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()

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

bruno.desthuilliers

On 2 Apr, 15:15, (e-mail address removed) wrote:
On 2 Apr, 15:12, (e-mail address removed) wrote:
On Apr 2, 3:06 pm, (e-mail address removed) wrote:
Hi,
I found the following code on the net -
http://mail-archives.apache.org/[email protected]>
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
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
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()

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

It's demonstration code.
 
B

bruno.desthuilliers

I found the following code on the net -

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?

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.
 
B

bruno.desthuilliers

On 2 Apr, 15:12, (e-mail address removed) wrote:
On Apr 2, 3:06 pm, (e-mail address removed) wrote:
Hi,
I found the following code on the net -
http://mail-archives.apache.org/[email protected]>
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
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,

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

def ExecuteWithNoFetching(self, queryString):

sqlServerConnection = adodbapi.connect (";".join (connectors))

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...
 
C

Carl Banks

Hi,

I found the following code on the net -

http://mail-archives.apache.org/[email protected]>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top