Python seems to be ignoring my except clause...

A

Adam W.

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.
 
D

Duncan Booth

Adam W. said:
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.
 
A

Adam W.

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.
 
J

Jonathan Gardner

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...
 
L

Larry Bates

Adam said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top