Python seems to be ignoring my except clause...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam W.

    Python seems to be ignoring my except clause...

    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.appen d(string.encode ())
    except:
    self.data.appen d('No habla la Unicode')

    And the exception:

    File "C:\Users\Adam\ Desktop\XMLWork space.py", line 65, in characters
    try:
    UnicodeEncodeEr ror: '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.
  • Duncan Booth

    #2
    Re: Python seems to be ignoring my except clause...

    "Adam W." <AWasilenko@gma il.comwrote:
    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.appen d(string.encode ())
    except:
    self.data.appen d('No habla la Unicode')
    >
    And the exception:
    >
    File "C:\Users\Adam\ Desktop\XMLWork space.py", line 65, in characters
    try:
    UnicodeEncodeEr ror: '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.

    Comment

    • Jonathan Gardner

      #3
      Re: Python seems to be ignoring my except clause...

      On Feb 19, 6:14 am, "Adam W." <AWasile...@gma il.comwrote:
      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...

      Comment

      • Larry Bates

        #4
        Re: Python seems to be ignoring my except clause...

        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.appen d(string.encode ())
        except:
        self.data.appen d('No habla la Unicode')
        >
        And the exception:
        >
        File "C:\Users\Adam\ Desktop\XMLWork space.py", line 65, in characters
        try:
        UnicodeEncodeEr ror: '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.appen d(string.encode ())
        except UnicodeEncodeEr ror:
        self.data.appen d('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

        Comment

        Working...