Regarding exception handling

Dan Perl danperl at rogers.com
Mon Jan 31 01:24:06 CET 2005


"Aggelos I. Orfanakos" <aorfanakos at gmail.com> wrote in message 
news:1107127179.863445.55310 at c13g2000cwb.googlegroups.com...
> Thanks. This should now be OK:
>
> #try:
> #    try:
> #        s = ... # socket opens
> #
> #        # various code ...
> #    except socket.error, x:
> #        # exception handling
> #finally:
> #    s.close() # socket closes
>

Why the nested try's?  If I understand your intention correctly this should 
do the same thing:

try:
     s = ... # socket opens
     # various code ...
except socket.error, x:
     # exception handling
s.close() # socket closes

You would need the try-finally statement only if you expect it to handle 
other exceptions than socket.error or if the socket.error is re-raised in 
the "except" clause.  Otherwise, the try-finally statement is redundant 
because no socket.error are getting out of the try-except statement.

Dan 





More information about the Python-list mailing list