Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python: how to catch exception and print message

Posted on 2013-05-20
3
Medium Priority
?
2,344 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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 …
Suggested Courses

715 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