catch UnicodeDecodeError
<p>very often I have the following problem: I write a program that processes many files which it assumes to be encoded in utf-8. Then, some day, I there is a non-utf-8 character in one of several hundred or thousand (new) files. The program exits with an error message like this:</p>

<p>UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 60: invalid continuation byte</p>

<p>I usually solve the problem by moving files around and by recoding them.</p>

<p>What I really want to do is use something like</p>

<pre><code>try:

    # open file, read line, or do something else, I don't care

except UnicodeDecodeError:

    sys.exit("Found a bad char in file " + file + " line " + str(line_number)
</code></pre>

<p>Yet, no matter where I put this try-except, it doesn't work.</p>

<p>How should I use try-except with UnicodeDecodeError?</p>
