Odp: What about try:except:finally

Tomek Lisowski Lisowski.Tomasz at sssa.nospam.pl
Wed Jul 12 05:11:04 EDT 2000


Użytkownik Olivier Dagenais <olivierS.dagenaisP at canadaA.comM> 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