What about try:except:finally

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Wed Jul 12 08:05:56 EDT 2000


Yes, I was going on the assumption that #init wasn't going to throw an
exception, and that the inner "except" block would re-throw if it was really
bad:

try:
    # safe init stuff
    try:
        # not so safe stuff
    except:
        # is it an exception we want to handle?
        # if so, handle, send an error message, etc..
        # otherwise, re-throw the error
finally:
    # de-init stuff


--
----------------------------------------------------------------------
Olivier A. Dagenais - Carleton University - Computer Science III


"Tomek Lisowski" <Lisowski.Tomasz at sssa.nospam.pl> wrote in message
news:ICWa5.46250$Qw1.1084910 at news.tpnet.pl...
> U?ytkownik Olivier Dagenais w wiadomo?ci
> do grup dyskusyjnych
> napisa?:rkl95.48923$W35.1068917 at news20.bellglobal.com...
> > If I remember the tutorial correctly, it doesn't support finally with
> > anything else.  I have a feeling you would need to do:
> >
> > try:
> >     #init
> >     try:
> >        # stuff
> >     except:
> >        # stuff
> > finally:
> >     #stuff
> >
> > ...that is, assuming that your "except" would only catch specific
> exceptions
> > that you would want to prepare an error message with.
>
> Please quote the original message next time.
>
> At least I can see in your example one error. If you place try...except...
> within try...finally..., the latter construct won't see any exceptions at
> all. Moreover, if #init raises an exception, it won't be caught!
>
> I see it more logical in this order:
> try:
>    try:
>      #init
>      #stuff
>    except:
>      #stuff
> finally:
>    #stuff
>
> If you want separate behaviour for exceptions in #init and the first
#stuff
> sections, add yet another try...except... contruct. If #init is safe, and
> will not raise any exceptions, it can be placed outside of the inner
> try...except... construct.
>
> Tomek Lisowski
>





More information about the Python-list mailing list