开发者

python: Should I use ValueError or create my own subclass to handle invalid strings? [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Which exception should I raise on bad/illegal argument combinations in Python?

I've looked through python's built in exceptions and t开发者_开发知识库he 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.


ValueError is 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.


ValueError seems logical:

In [1]: int('abc')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

<ipython console> in <module>()

ValueError: invalid literal for int() with base 10: 'abc'


I vote for creating a unique subclass, InvalidFormatException.

ValueError, while true, is vague.

InvalidFormatException is more specific and tied directly to your problem.

A user could wind up with situations where they're doing something that could produce either error. They could be converting roman numerals and then doing some math. They might need to distinguish between the ValueError and the InvalidFormatException.


Well really it depends whether you want (or need) that particular exception to be catchable independently of other ValueErrors that may occur during invocation of your code. It also depends whether you are the sole consumer of your code or it's intended for other people to use; in the latter case it may be helpful to these people if you define some high-level library-specific exceptions that they can check for.


Yes. ;-) ValueError does sound like the most applicable of the built-in ones, and with a subclass of that you seem to be doing the best possible. It's also what f.i. '%q' % 1 would raise.


I also support ValueError for this case. My 2 cents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜