Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python: how to catch exception and print message

Posted on 2013-05-20
3
Medium Priority
?
2,468 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

885 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