Python seems to be ignoring my except clause...

Discussion in 'Python' started by Adam W., Feb 19, 2008.

  1. Adam W.

    Adam W. Guest

    I am trying to handle a Unicode error but its acting like the except
    clause is not even there. Here is the offending code:

    def characters(self, string):
    if self.initem:
    try:
    self.data.append(string.encode())
    except:
    self.data.append('No habla la Unicode')

    And the exception:

    File "C:\Users\Adam\Desktop\XMLWorkspace.py", line 65, in characters
    try:
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
    position 83: ordinal not in range(128)

    Its got to be something really dumb I'm missing, this make no sence.
    Adam W., Feb 19, 2008
    #1
    1. Advertising

  2. Adam W.

    Duncan Booth Guest

    "Adam W." <> wrote:

    > I am trying to handle a Unicode error but its acting like the except
    > clause is not even there. Here is the offending code:
    >
    > def characters(self, string):
    > if self.initem:
    > try:
    > self.data.append(string.encode())
    > except:
    > self.data.append('No habla la Unicode')
    >
    > And the exception:
    >
    > File "C:\Users\Adam\Desktop\XMLWorkspace.py", line 65, in characters
    > try:
    > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
    > position 83: ordinal not in range(128)
    >
    > Its got to be something really dumb I'm missing, this make no sence.
    >


    The example you posted isn't complete and while I can easily expand it to a
    working example it will unfortunately be a working example.

    Try cutting it down yourself to a minimal self-contained example that you
    can post. 99% of the time you'll find the problem when you do that and
    avoid having to post at all.

    In this case, judging by the stack backtrace quoting the wrong line, I'd
    guess you only just added the try..except and for some reason are still
    executing the old code without the exception handling.
    Duncan Booth, Feb 19, 2008
    #2
    1. Advertising

  3. Adam W.

    Adam W. Guest

    On Feb 19, 8:49 am, Duncan Booth <> wrote:
    > The example you posted isn't complete and while I can easily expand it to a
    > working example it will unfortunately be a working example.
    >
    > Try cutting it down yourself to a minimal self-contained example that you
    > can post. 99% of the time you'll find the problem when you do that and
    > avoid having to post at all.
    >
    > In this case, judging by the stack backtrace quoting the wrong line, I'd
    > guess you only just added the try..except and for some reason are still
    > executing the old code without the exception handling.- Hide quoted text -
    >
    > - Show quoted text -


    You hit the nail on the head with the old code, I found it funny
    myself it was quoting the try statement and not the code within. So I
    deleted my .pyc files and reran, same thing, but then I closed all
    open windows and reran it, and it recompiled the pyc and the code
    "worked". But now there is a new problem. I added a print statement
    to the except clause to make sure it was executing, and something
    funny happen, it printed 3 times and then spat this out:

    File "C:\Users\Adam\Desktop\XMLWorkspace.py", line 72, in parsexml
    parse(url, FeedHandlerInst)
    File "C:\Python25\lib\xml\sax\__init__.py", line 33, in parse
    parser.parse(source)
    File "C:\Python25\lib\xml\sax\expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
    File "C:\Python25\lib\xml\sax\xmlreader.py", line 123, in parse
    self.feed(buffer)
    File "C:\Python25\lib\xml\sax\expatreader.py", line 211, in feed
    self._err_handler.fatalError(exc)
    File "C:\Python25\lib\xml\sax\handler.py", line 38, in fatalError
    raise exception
    SAXParseException: http://revision3.com/systm/feed/wmv-large/:78:83:
    undefined entity

    Huh? Why did it not raise this BEFORE it attempted to append the
    string, why did my print statment print 3 times before this error? I
    think I got past the hump I was hitting, and found a new one, 3 items
    later, I will investigate. But now I know I have to keep deleting my
    pyc files or else I will run into trouble.
    Adam W., Feb 19, 2008
    #3
  4. On Feb 19, 6:14 am, "Adam W." <> wrote:
    > So I deleted my .pyc files and reran, same thing, but then I closed all
    > open windows and reran it, and it recompiled the pyc and the code
    > "worked".
    > ...
    > But now I know I have to keep deleting my
    > pyc files or else I will run into trouble.


    What editor are you using? It sounds like it doesn't set the timestamp
    on the files you are editing properly. That is, every time you save
    your file it should update the timestamp of the .py file so that
    python can see that there is an older .pyc next to a newer .py.

    But that is probably the least of your worries right now...
    Jonathan Gardner, Feb 19, 2008
    #4
  5. Adam W.

    Larry Bates Guest

    Adam W. wrote:
    > I am trying to handle a Unicode error but its acting like the except
    > clause is not even there. Here is the offending code:
    >
    > def characters(self, string):
    > if self.initem:
    > try:
    > self.data.append(string.encode())
    > except:
    > self.data.append('No habla la Unicode')
    >
    > And the exception:
    >
    > File "C:\Users\Adam\Desktop\XMLWorkspace.py", line 65, in characters
    > try:
    > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
    > position 83: ordinal not in range(128)
    >
    > Its got to be something really dumb I'm missing, this make no sence.


    Seems that others have addressed you specific problem so I wanted to take this
    opportunity to save you from hours of frustration in the future (trust me on
    this one). It is almost NEVER a good idea to use a blank except: clause in your
    code. The problem is that it catches ALL exceptions, not just the one you are
    trying to catch. It is much better to determine the exception you are trying to
    catch here.

    Example:

    try:
    self.data.append(string.encode())
    except UnicodeEncodeError:
    self.data.append('No habla la Unicode')


    You can spend a lot of time trying to figure out what is going on when your
    blank except catches all the exceptions. This lets the other exceptions do what
    they should do.

    Hope this helps.

    Larry Bates
    Larry Bates, Feb 20, 2008
    #5
    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. George Young

    blanket except clause -- best practice?

    George Young, Oct 28, 2003, in forum: Python
    Replies:
    3
    Views:
    400
    John J. Lee
    Oct 29, 2003
  2. Bob Alistar
    Replies:
    0
    Views:
    461
    Bob Alistar
    Dec 4, 2003
  3. Derek Schuff

    except clause not catching IndexError

    Derek Schuff, Feb 22, 2006, in forum: Python
    Replies:
    7
    Views:
    532
    Sion Arrowsmith
    Feb 23, 2006
  4. Replies:
    2
    Views:
    228
  5. Nebur
    Replies:
    5
    Views:
    308
    Gabriel Genellina
    May 30, 2007
Loading...

Share This Page