Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Exception not raised

Reply
Thread Tools

Exception not raised

 
 
Michele Petrazzo
Guest
Posts: n/a
 
      02-24-2006
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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-24-2006
Michele Petrazzo wrote:

> 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!



Not here:

t_fields = {}
#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

Gives me

python2.4 /tmp/test.py
ok
<type 'dict'> False
Traceback (most recent call last):
File "/tmp/test.py", line 9, in ?
test()
File "/tmp/test.py", line 5, in test
print t_fields[11]
KeyError: 11


So - whatever you do, there must be some other code capturing that
exception. Maybe you don't use dict, but a subclass of it?

Diez
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      02-24-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
 
Reply With Quote
 
Michele Petrazzo
Guest
Posts: n/a
 
      02-24-2006
Diez B. Roggisch wrote:
> Not here:
>
> t_fields = {}
> #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
>
> Gives me
>
> KeyError: 11


Also on my environ when I try this 4 line code. My project, where that
strangeness happen has 500/600 + K of code.

>
>
> So - whatever you do, there must be some other code capturing that
> exception.


This code are inside a method into class that have no try/except. And
called from a method inside a wx.Frame derivate. The other strange thing
is that if I try the same code just before the "caller" to that method,
it raise an exception:

#code extract alway from the big project:

class errorHappen(object):
def methodError(self, t_fields):
print type(t_fields), 11 in t_fields
print t_fields[11]
print "I'm here"


def myF(wx.Frame):
.... init ....
self._e = errorHappen()

def caller(self):
d = dict(dictionary with 50/100 keys)
#if here I try d[11], I'll KeyError
self._e.methodError(d) #here I don't see the exeception


>Maybe you don't use dict, but a subclass of it?


All my dict are created as
d = dict()

and populate by: d.update(other_dict) or for k, val in iter: d[k] = val

>
> Diez



Thanks,
Michele
 
Reply With Quote
 
Michele Petrazzo
Guest
Posts: n/a
 
      02-24-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:


Just for give evidence to my _failed_ tests, my a debugger (eric3), it
see the exception, so it break with a KeyError!
And the same code, no!

Thanks,
Michele
 
Reply With Quote
 
Michele Petrazzo
Guest
Posts: n/a
 
      02-24-2006
Michele Petrazzo wrote:
> 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:

>


Opss, I forgot some words

> Just for give evidence to my _failed_ tests, my a debugger (eric3), it


with a debugger (not my a debugger)

> see the exception, so it break with a KeyError!
> And the same code, no!


And the same code, exceute inside a terminal, no...
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-24-2006
> This code are inside a method into class that have no try/except. And
> called from a method inside a wx.Frame derivate. The other strange thing
> is that if I try the same code just before the "caller" to that method,
> it raise an exception:


So maybe the C-layer of wx in-between doesn't propagate the exception for whatever reason? Fact is: pythons
exception-handling is core part of the language and an error in there _extremely_ improbable . So I suggest you cut down
your example until it is self-contained with only a few dozen lines and still produces your observed behavior. Otherwise
no one will be able to help you.

Diez
 
Reply With Quote
 
Michele Petrazzo
Guest
Posts: n/a
 
      02-25-2006
Diez B. Roggisch wrote:
>> This code are inside a method into class that have no try/except.
>> And called from a method inside a wx.Frame derivate. The other
>> strange thing is that if I try the same code just before the
>> "caller" to that method, it raise an exception:

>
> So maybe the C-layer of wx in-between doesn't propagate the exception
> for whatever reason?


Can be, but only that exception aren't raised, all the other, in the
same point of code, yes.

> Fact is: pythons exception-handling is core part of the language and
> an error in there _extremely_ improbable .


I think the same, and this is one of the reason that push me to use python.

> So I suggest you cut down your example until it is self-contained
> with only a few dozen lines and still produces your observed
> behavior. Otherwise no one will be able to help you.


Into my 100 line code, that exception (and all the others) are raised! I
don't able, into all my tries, to reproduce that error on a small code...

I can publish my code, if someone has one hour of spare time and a
postgresql where test the code. I'm make some instruction to reproduce it.

>
> Diez


Thanks,
Michele
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception not raised - May be the end Michele Petrazzo Python 2 03-03-2006 12:23 PM
Page not displaying when exception raised pbo.spam@gmail.com ASP .Net 5 11-07-2005 02:13 PM
: an exception raised =?Utf-8?B?SklNLkgu?= ASP .Net 9 06-03-2005 03:22 PM
exception raised for valid variable declaration? Carlos ASP .Net 1 02-17-2005 08:26 PM
Exception feature creep! (was: re-entering in the normal flow after an exception is raised) Lonnie Princehouse Python 8 10-02-2004 09:16 PM



Advertisments