try/except/finally

Marko Rauhamaa marko at pacujo.net
Sun Jun 8 03:12:54 EDT 2014


Mark Lawrence <breamoreboy at yahoo.co.uk>:

>>> A return statement inside a finally block is code smell.
>> Not to my nose.  It seems like a perfectly reasonable thing to do.
> I agree, the code smell is the return in the except block.

Here's a regular pattern that I use for nonblocking I/O:

    def poll(self):
        try:
            message = self.sock.recv(0x10000)
        except IOError as e:
            if e.errno == errno.EAGAIN:
                return
            if errcode == errno.EINTR:
                self.trigger()
                return
            self.handle_io_error(e.errno)
            return
        self.trigger()
        self.handle_recv(message)

Does anyone have an example motivating a return from finally? It seems
to me it would always be a bad idea as it silently clears all unexpected
exceptions.


Marko



More information about the Python-list mailing list