473,378 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Exception not raised

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
Feb 24 '06 #1
7 1563
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
Feb 24 '06 #2
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
Feb 24 '06 #3
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
Feb 24 '06 #4
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
Feb 24 '06 #5
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...
Feb 24 '06 #6
> 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
Feb 24 '06 #7
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
Feb 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: William Park | last post by:
(crossposted to comp.lang.python, because this may be of interest to them.) Python has try-block, within which you can raise exception. Once it's raised, execution breaks out of the try-block...
8
by: Lonnie Princehouse | last post by:
In a recent post, Michele Simionato asked about resumable (or re-entrant) exceptions, the basic idea being that when raised and uncaught, these exceptions would only propagate backwards through a...
8
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
3
by: MackS | last post by:
Hi I'm new to Python and would like to know if the following is possible. Say I have one lower-level object A and one user-interface object B. Suppose B.CallingMethod() calls A.CalledMethod(),...
3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
3
by: elziko | last post by:
I have a procedure that creates a bitmap of a certain size and then displays it in a 3rd party component. However, if the bitmap is very large then a System.OutOfMemoryException is thrown my...
1
by: Bob | last post by:
In Vs 2005 you have new applicationsEvents.vb I was testing it in a simple app and found that it was easier to implement unhandled exception management tah it was in Vs2003 (vb.net) You can, if you...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
5
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I feel like a noob for asking this. When I publish a VB windows application, I want to disable the ability of the the user to continue when there is an unhandled exception. For example,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.