Regarding exception handling

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


Hello.

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:

try:
try:
s = ... # socket opens

# various code ...
except socket.error, x:
# exception handling
finally:
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