[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python seems to be ignoring my except clause...

Adam W.

2/19/2008 1:42:00 PM

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.
4 Answers

Duncan Booth

2/19/2008 1:49:00 PM

0

"Adam W." <AWasilenko@gmail.com> 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.

Jonathan Gardner

2/19/2008 7:47:00 PM

0

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

Larry Bates

2/20/2008 2:09:00 PM

0

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

jat

6/6/2011 12:02:00 PM

0

On 06/06/2011 07:16 AM, observador wrote:
>
> "Yankee, Go Home", Be very carefull of what you hope for, because you
> might get your wish.......
> Cuba 2011, "Yankees, please come".....
> Or Just Maybe, Venezuela 2012 .... "Yankees Please Come for our
> Oil."..jejejeje
>
>
>
Let's see what happens then. In the meanwhile "Yankee, Go Home". They
have many things to do out there...

--
/jat
Knowledge shall make you free
El conocimiento te har? libre