Nested try...except

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Wed Apr 2 19:03:57 EDT 2008


On 2 avr, 15:12, cokofree... at gmail.com wrote:
> On Apr 2, 3:06 pm, Magnus.Morab... at 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.



More information about the Python-list mailing list