[Python-bugs-list] [ python-Bugs-701135 ] except: grammar is incorrect
SourceForge.net
noreply@sourceforge.net
Tue, 11 Mar 2003 02:37:10 -0800
Bugs item #701135, was opened at 2003-03-10 21:15
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=701135&group_id=5470
Category: Documentation
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Mark Ferris (mferris1)
>Assigned to: Michael Hudson (mwh)
Summary: except: grammar is incorrect
Initial Comment:
In the Python Language Reference manual, it shows the try-
except grammar as:
try_exc_stmt::= "try" ":"
suite
("except" [expression ["," target]] ":"
suite)+
This implies that this is a valid except
stanza:
except TypeError or VaueError or
KeyError:
This syntax does not produce any errors, but it
doesn't work as one would expect either. It only "excepts" the first
exception in the expression; i.e. TypeError in this
example.
I am assuming the grammar doc just needs to be
updated and this is not the intented syntax for except.
----------------------------------------------------------------------
>Comment By: Michael Hudson (mwh)
Date: 2003-03-11 10:37
Message:
Logged In: YES
user_id=6656
No, the grammar is correct.
>>> TypeError or VaueError or KeyError
<class exceptions.TypeError at 0x8106504>
It may not be what you expected, but it's what's allowed:
/>> def e():
|.. import random
|.. return random.choice([KeyError, TypeError])
\__
/>> def f():
|.. try:
|.. {}[2]
|.. except e():
|.. print "caught 1"
|.. except:
|.. print "caught 2"
\__
->> f()
caught 1
->> f()
caught 1
->> f()
caught 2
->> f()
caught 2
->> f()
caught 1
->>
... and so on.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=701135&group_id=5470