Possible Duplicate:
Which exception should I raise on bad/illegal argument combinations in Python?
I’ve looked through python’s built in exceptions and the only thing that seems close is ValueError.
from python documentation:
exception ValueError:
Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
Should I create a subclass of ValueError, like InvalidFormatException?
(My particular case is if a roman numeral string is improperly formatted, but there are many other applicable cases.)
EDIT: it seems like ValueError is the right choice, now the issue is whether to use ValueError directly or to subclass it.
ValueErroris a good match for the case you have. Just go with that and remember that you can specify a useful message as an argument, letting you distinguish this from other types of ValueError.I would not make the code more complicated by defining a subclass, however, unless I had a good reason to want to catch just that particular error but avoid catching any other ValueErrors. Many applications have dozens of “special” error conditions, but if they also defined per-case subclasses the code would quickly get unmaintainable and anyone trying to use the routines would be constantly surprised by the unexpected new exceptions.