except clause syntax question
Mel Wilson
mwilson at the-wire.com
Mon Jan 30 14:15:04 EST 2012
Charles Yeomans wrote:
> To catch more than one exception type in an except block, one writes
>
> except (A, B, C) as e:
>
> I'm wondering why it was decided to match tuples, but not lists:
>
> except [A, B, C] as e:
>
> The latter makes more sense semantically to me -- "catch all exception
> types in a list" as opposed to "catch this single thing composed of three
> exception types".
I've always been perfectly fine with sometimes treating tuples as immutable
sequences, so I'm +0 on saying "more sense semantically", but given that the
exception list can be a variable, I'm not sure what the gain is by keeping
it immutable.
#-----------------------------------------------------------
#!/usr/bin/env python
# -*- coding: ASCII -*-
'''Demonstrate catching variable exceptions.
'''
def excepter (a, exceptions):
try:
1.0/a
'Number ' + a
except exceptions as e:
print '!!! *** EXCEPTER CAUGHT ONE *** !!!'
print repr (e)
#~ excepter (0, [ZeroDivisionError])
excepter (0, (ZeroDivisionError,))
excepter (1, (ZeroDivisionError,TypeError))
excepter (1, (ZeroDivisionError,))
#-----------------------------------------------------------
excepter called with the list catches nothing, of course.
Mel.
More information about the Python-list
mailing list