Exception not raised
Larry Bates
larry.bates at websafe.com
Fri Feb 24 12:32:16 EST 2006
Michele Petrazzo wrote:
> Hi list, I have a strange error on my software on win 2k/xp and debian
> 3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython:
>
> python, on a piece of code doesn't raise a KeyError on a dict (that
> don't have that key), but the strange thing is that the try/except code
> see that exception. Other strange thing is that other exceptions are
> raised!
>
> Simple example extract from my code:
>
> #code
> def test():
> print type(t_fields), 11 in t_fields
> print t_fields[11]
> print "I'm here"
>
> print "ok"
> test()
> print "ok"
> #end code
>
> Output:
>
> ok
> <type 'dict'> False
>
> Here I see only one "ok" and not the "I'm here". The interpreter stop to
> run here, but the application continue to work!
>
> Other try:
>
> #code
> def test():
> try:
> print type(t_fields), 11 in t_fields
> print t_fields[11]
> except KeyError, ex:
> print "Error", ex
> #end code
>
> Output:
>
> ok
> <type 'dict'> False
> Error 11
> ok
>
> Here the output is ok, so python see that exception inside the
> try/except and print it.
>
> Last try:
>
> #code
> def test()
> print type(t_fields), 11 in t_fields
> print dont_exist
> print t_fields[11]
> #end code
>
> Output:
>
> ok
> <type 'dict'> False
> File "conn.py", line 231, in test
> print dont_exist
> NameError: global name 'dont_exist' is not defined
>
>
> So all the exception are raised except the KeyError outside the try/except!
>
> I don't know what can be.
>
> Thanks to all that can help me.
> Michele
When I run your first example I get:
>>> <type 'dict'> False
Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\larry\My Documents\junk.py", line 14, in ?
test()
File "C:\Documents and Settings\larry\My Documents\junk.py", line 8, in test
print t_fields[11]
KeyError: 11
>>>
I don't know why you see anything different than a traceback when you
try to access t_fields[11] (which doesn't exist).
-Larry Bates
More information about the Python-list
mailing list