[Python-ideas] Default return values to int and float
Jan Kaliszewski
zuo at chopin.edu.pl
Sat Oct 8 22:17:13 CEST 2011
Steven D'Aprano dixit (2011-10-08, 22:23):
> Ron Adam wrote:
[snip]
> >What if you can catch an exception specifically from a particular
> >function or method, but let other unexpected "like" exceptions bubble
> >through...
> >
> > try:
> > ...
> > i = s.index('bar')
> > ...
> > except ValueError from s.index as exc:
> > <handle s.index ValueError>
>
>
> I can't imagine that this would even be *possible*, but even if it
> is, I would say it certainly isn't *desirable*.
>
> (1) You're repeating code you expect to fail: you write s.index
> twice, even though it only gets called once.
>
> (2) The semantics are messy and unclear. Suppose you have this:
>
> try:
> ...
> i = s.index(a)
> j = s.index(b) + s.index(c)
> t = s
> k = t.index(d)
> method = s.index
> l = method(e)
> ...
> except ValueError from s.index:
> ...
>
> Which potential s.index exceptions will get caught? All of them? Some
> of them? Only i and j? What if you want to catch only some but not
> others?
[snip]
Maybe labeling interesting lines of code could be more suitable? E.g.:
try:
...
'risky one'
i = s.index('bar')
...
except ValueError from 'risky one' as exc:
<handle ValueError raised at the line preceded by lone
'risky one' string literal>
Cheers.
*j
More information about the Python-ideas
mailing list