
On Dec 28, 2013, at 20:39, Bruce Leban <bruce@leapyear.org> wrote:
On Sat, Dec 28, 2013 at 12:05 PM, Andrew Barnert <abarnert@yahoo.com> wrote:
Can you put an exception type or tuple between the "except try"? What about an as clause (so the block could use it)?
I started writing a mail with exactly this subject line and discarded it because of the latter two issues.
The second one is pretty minor; it's just a matter of making the syntax clean, and I'm sure someone would come up with something if it were worth doing. The part that worries me about the first issue is that there seem to be two reasonable ways to read it. In your example below, if the first attempt raises an AttributeError, should it go to the "except AttributeError try:" block, or should that only happen if the second attempt raises one? The first variation means it's _not_ just syntactic sugar for the overly indented form you gave before it. But that also means (I think) it requires a change to the way exceptions are handled. (I'd have to sit down and work out the bytecode to be sure about that.) And, more importantly, I don't think the meaning would be obvious to a reader--the fact that you assumed the other meaning was the only one possible is pretty strong evidence for that.
In code I've recently written I have a chain of three and while the indentation is ugly, it's not so ugly that it cries out as a must fix language feature. And the problem is that I really want to limit to specific exceptions. In one case I have something like this:
try: .... except KeyError: try: .... except AttributeError: try: .... except IndexError: ....
and if I can't specify the specific exception, this is not helpful. On the other hand
try: .... except KeyError try: .... except AttributeError try: .... except IndexError: ....
only really saves me two colons and some whitespace. If the language had this feature, I'd use it, but that doesn't make it worth adding.
--- Bruce