Guessing the encoding from a BOM

Ethan Furman ethan at stoneleaf.us
Thu Jan 16 02:29:15 EST 2014


On 01/15/2014 10:55 PM, Steven D'Aprano wrote:
> On Thu, 16 Jan 2014 14:47:00 +1100, Ben Finney wrote:
>>
>> +1. I'd like a custom exception class, sub-classed from ValueError.
>
> Why ValueError? It's not really a "invalid value" error, it's more "my
> heuristic isn't good enough" failure. (Maybe the file starts with another
> sort of BOM which I don't know about.)
>
> If I go with an exception, I'd choose RuntimeError, or a custom error
> that inherits directly from Exception.

 From the docs [1]:
============================

     exception RuntimeError

         Raised when an error is detected that doesn’t fall in any
         of the other categories. The associated value is a string
         indicating what precisely went wrong.

It doesn't sound like RuntimeError is any more informative than Exception or AssertionError, and to my mind at least is 
usually close to catastrophic in nature [2].

I'd say a ValueError subclass because, while not an strictly an error, it is values you don't know how to deal with. 
But either that or plain Exception, just not RuntimeError.

--
~Ethan~


[1] http://docs.python.org/3/library/exceptions.html#RuntimeError
[2] verified by a (very) brief grep of the sources



More information about the Python-list mailing list