Exception not raised

Discussion in 'Python' started by Michele Petrazzo, Feb 24, 2006.

  1. 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
     
    Michele Petrazzo, Feb 24, 2006
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Feb 24, 2006
    #2
    1. Advertising

  3. Michele Petrazzo

    Larry Bates Guest

    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
     
    Larry Bates, Feb 24, 2006
    #3
  4. 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
     
    Michele Petrazzo, Feb 24, 2006
    #4
  5. 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
     
    Michele Petrazzo, Feb 24, 2006
    #5
  6. 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...
     
    Michele Petrazzo, Feb 24, 2006
    #6
  7. > 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
     
    Diez B. Roggisch, Feb 24, 2006
    #7
  8. 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
     
    Michele Petrazzo, Feb 25, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos
    Replies:
    1
    Views:
    372
    Wilco Bauwer
    Feb 17, 2005
  2. =?Utf-8?B?SklNLkgu?=

    : an exception raised

    =?Utf-8?B?SklNLkgu?=, Mar 2, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    455
    =?Utf-8?B?SGl0ZXNo?=
    Jun 3, 2005
  3. Replies:
    5
    Views:
    453
  4. Lonnie Princehouse
    Replies:
    8
    Views:
    551
    Lonnie Princehouse
    Oct 2, 2004
  5. Michele Petrazzo

    Exception not raised - May be the end

    Michele Petrazzo, Mar 2, 2006, in forum: Python
    Replies:
    2
    Views:
    312
    Michele Petrazzo
    Mar 3, 2006
Loading...

Share This Page