Regarding exception handling

Aggelos I. Orfanakos aorfanakos at
Sun Jan 30 14:47:18 EST 2005


In a program, I want to ensure that a socket closes (so I use try ...
finally), but I also want to catch/handle a socket exception. This is
what I have done:

s = ... # socket opens

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

Is there a more "elegant" or "proper" way to do this? Or the above code
is OK?

Thanks in advance.

More information about the Python-list mailing list