except block isn't catching exception

Chris Angelico rosuav at gmail.com
Sat Aug 8 04:44:31 CEST 2015


On Sat, Aug 8, 2015 at 3:16 AM,  <sohcahtoa82 at gmail.com> wrote:
>
> Though I still doesn't understand why the exception isn't caught when I'm explicitly trying to catch it.  I even tried changing the try/except block to this:
>
> try:
>     connection, _ = sock.accept()
> except KeyboardInterrupt:
>     print 'KeyboardInterrupt caught!'
> except socket.timeout:
>     print 'Socket timeout caught!'
> except:
>     print 'other exception caught!'
> finally:
>     print 'finally!'
>
> The result prints:
>
> Interrupting main
> main interrupted!
> finally!

The exception isn't happening inside sock.accept(), as I explained. So
you can't catch it there.

ChrisA


More information about the Python-list mailing list