Solved

Python: how to catch exception and print message

Posted on 2013-05-20
3
1,738 Views
Last Modified: 2013-05-21
If the file "fun.txt" does not exist then the error message is printed. However, I also want to catch UnicodeDecodeError but that isn't working.

try:
    stream = codecs.open("Fun.txt", 'rb', encoding='utf-8')
    for line in stream:
        print line
        print type(line)

except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror) //works
except UnicodeDecodeError as ude:
    print "UnicodeDecodeError({0}): {1}".format(ude.errno, ude.strerror) // doesn't work

ERROR MESSAGE:
Traceback (most recent call last):
  File "C:/junk/python/exceptions/exceptions.py", line 16, in <module>
    print "UnicodeDecodeError({0}): {1}".format(ude.errno, ude.strerror)
AttributeError: 'exceptions.UnicodeDecodeError' object has no attribute 'errno'

Here is the error message I am trying to catch:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf8 in position 6: invalid start byte

If fun.txt has the following in it:
Hello World, frøm other encodings

and it is saved as ASCII then it will produce the UnicodeDecodeError message you see above. I want to catch that error message and print it to standard output.
0
Comment
Question by:alexmac05
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39183134
You're catching it fine.  It's just that a UnicodeDecodeError doesn't have strerror or errno properties.  See http://docs.python.org/2/library/exceptions.html#exceptions.UnicodeError for the supported properties of a UnicodeDecodeError.

If you're after the str representation of the error then:

 print "UnicodeDecodeError({0})".format(ude)
0
 

Author Comment

by:alexmac05
ID: 39185084
Turns out i had to do this in python 2.3.4

try:
    stream = codecs.open("Fun.txt", 'rb', encoding='utf-8')
    for line in stream:
        print line
        print type(line)
except IOError, err:
    print err
except UnicodeDecodeError, err:
    print err
except Exception, err:
    print err
else:
    stream.close()
    print 'file close'
0
 

Author Closing Comment

by:alexmac05
ID: 39185086
Thank you very much. Very good answer
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question