Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Coming from C, I think that it's generally a good programming practice to make sure everything you create, closes; whether it's about a socket or a file. This may not be the case with Python though. To be honest, leaving this task to the garbage collector doesn't sound like a good idea to me (since

Re: Regarding exception handling

2005-01-30 Thread Bryan
Fredrik Lundh wrote: "Bryan" wrote the above is not the same. make the a = ... raise an exception and you'll see the difference. s = ... # a = 1/0 s.close() as you can see, s.close() will never be called. also, in this example, i intentionally didn't put the extra try/except around the try/fina

Re: Regarding exception handling

2005-01-30 Thread Fredrik Lundh
"Bryan" wrote > the above is not the same. make the a = ... raise an exception and you'll see > the difference. > > s = ... # > a = 1/0 > s.close() > > as you can see, s.close() will never be called. also, in this example, i > intentionally didn't put > the extra try/except around the try/fina

Re: Regarding exception handling

2005-01-30 Thread Bryan
Dan Perl wrote: "Bryan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] IMO, that is not the reason for the try/finally statement and it is not redundant. the try/finally statement guarantees the resource is closed and the try/finally statement only gets executed if and only if the

Re: Regarding exception handling

2005-01-30 Thread Dan Perl
"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Good point, but with your way, if "s = ... # socket opens" fails, then > nothing will catch it. What I usually do is what I wrote above (place > it below the 2nd try), and when attempting to close it, first use a

Re: Regarding exception handling

2005-01-30 Thread Dan Perl
"Bryan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > IMO, that is not the reason for the try/finally statement and it is not > redundant. the try/finally statement guarantees the resource is closed > and the try/finally statement only gets executed if and only if the > openin

Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Good point, but with your way, if "s = ... # socket opens" fails, then nothing will catch it. What I usually do is what I wrote above (place it below the 2nd try), and when attempting to close it, first use an if like: "if locals().has_key('s'):". -- http://mail.python.org/mailman/listinfo/python

Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
I need it because the "various code" may raise other exceptions (not related to sockets). In such case, the "except socket.error, x:" won't catch it, but thanks to the "finally:", it is sure that the socket will close. -- http://mail.python.org/mailman/listinfo/python-list

Re: Regarding exception handling

2005-01-30 Thread Bryan
Dan Perl wrote: "Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Thanks. This should now be OK: #try: #try: #s = ... # socket opens # ## various code ... #except socket.error, x: ## exception handling #finally: #s.close() # soc

Re: Regarding exception handling

2005-01-30 Thread Dan Perl
"Aggelos I. Orfanakos" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thanks. This should now be OK: > > #try: > #try: > #s = ... # socket opens > # > ## various code ... > #except socket.error, x: > ## exception handling > #finally: > #s.close()

Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
Thanks. This should now be OK: #try: #try: #s = ... # socket opens # ## various code ... #except socket.error, x: ## exception handling #finally: #s.close() # socket closes -- http://mail.python.org/mailman/listinfo/python-list

Re: Regarding exception handling

2005-01-30 Thread beliavsky
Aggelos I. Orfanakos wrote: > (I don't know why, but indentation was not preserved once I posted.) This problem and its possible solutions was discussed here in the thread "OT: spacing of code in Google Groups". -- http://mail.python.org/mailman/listinfo/python-list

Re: Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
(I don't know why, but indentation was not preserved once I posted.) -- http://mail.python.org/mailman/listinfo/python-list

Regarding exception handling

2005-01-30 Thread Aggelos I. Orfanakos
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