Solved

Python: how to catch exception and print message

Posted on 2013-05-20
3
1,983 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
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. …
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

734 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