10

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.

Community
  • 1
  • 1
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157

6 Answers6

11

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.

Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
  • 1
    This is the best answer (remember you can supply a bit of specific text with the exception, and it gets delivered with trackback); however, it wouldn't take much of a reason at all to push me to create a subclass---an extremely low, but still present, barrier, in other words. –  Jan 03 '10 at 00:19
  • @Roger, I agree, and good point about the message. I've edited to mention that useful idea. – Peter Hansen Jan 03 '10 at 01:32
  • 1
    Remember, the message is for human checking, sublcassing is for machine checking. It's better to subclass than fish the message out from `exc_info`, since you'll have to catch ValueError then exc_info on it etc. etc. If you think (if the question even comes up then the answer is almost always 'yes') somewhere later in your program you will need to distinguish which kind of domain-specific error happened, then subclass ValueError. – cowbert Mar 30 '18 at 21:33
4

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'
Ofri Raviv
  • 24,375
  • 3
  • 55
  • 55
4

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
3

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.

Antoine P.
  • 4,181
  • 1
  • 24
  • 17
-1

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.

Wim
  • 11,091
  • 41
  • 58
-1

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

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431