Solved

Python: how to catch exception and print message

Posted on 2013-05-20
3
1,584 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 …
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now