Which exception to use?

Bjorn Pettersen BPettersen at NAREX.com
Tue Jan 28 20:42:18 CET 2003

> From: Edward C. Jones [mailto:edcjones at erols.com] 
> Which exception is it best to use in the following pieces of code?
> if len(sys.argv) != 2:
>      raise SomeException, 'program requires exactly one argument'

Do you really want a traceback in this case?...

  print >> sys.stderr, 'program requires exactly one argument'

> if len(sometuple) != 3:
>      raise SomeException, 'sometuple must have length 3'
> I use TypeError or ValueError but they don't feel right. I wish there 
> was a SizeError or LengthError.

It would generally be a value error, however if you think a SizeError is
better, nothing is stopping you:

  class SizeError(ValueError): pass

-- bjorn

More information about the Python-list mailing list