Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one indicate invalid arguments in Python? [duplicate]

Possible Duplicate:
python: Should I use ValueError or create my own subclass to handle invalid strings?

Reading Built-in Exceptions I read:

All user-defined exceptions should also be derived from this class" with regards to Exception.

I also see a ValueError which says:

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.

If I want to raise an exception for invalid arguments (the equivalent of Ruby's ArgumentError), what should I do? Should I raise ValueError directly, or preferably subclass ValueError with my own intention revealing name?

In my case, I accept a key argument, but I want to restrict the set of characters in the key, such that only /\A[\w.]+\Z/ (Perl/Ruby regular expression) is accepted.

like image 277
François Beausoleil Avatar asked Feb 25 '11 02:02

François Beausoleil


People also ask

What is invalid argument error in Python?

The invalid argument error is a WebDriver error that occurs when the arguments passed to a command are either invalid or malformed. Invalid argument errors can be likened to TypeError s in JavaScript, in that they can occur for a great many APIs when the input value is not of the expected type or malformed in some way.

What causes value error in Python?

What Causes ValueError. The Python ValueError is raised when the wrong value is assigned to an object. This can happen if the value is invalid for a given operation, or if the value does not exist. For example, if a negative integer is passed to a square root operation, a ValueError is raised.

How do you define errors in Python?

In Python, users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class. Most of the built-in exceptions are also derived from this class.

Which exception occurs whenever an invalid argument is used in a function call in Python?

Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError , but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError .


2 Answers

I think the general idea is this: ValueError should almost always denote some sort of client error (where 'client' means the programmer using your interface). There are two high-level types of exceptions in Python:

  • uncommon cases for otherwise normally functioning code; the client is not to blame

  • usage errors where some interface is used incorrectly, or the through a series of interface calls, the system has reached an inconsistent state; time to blame the client

In my opinion, for the first case, it makes sense to create exception class hierarchies to allow client code fine-grained control over what to do in uncommon cases.

In the second case, and ValueError is an example of this, you are telling the client that they did something wrong. Fine-grained exception hierarchies are not as important here, because client code probably should be fixed to do the right thing (e.g., pass the right parameter types in the first place).

TL;DR: Just use ValueError, but include a helpful message (e.g., raise ValueError("I'm afraid I can't let you do that, Dave. -HAL 9000"). Don't subclass unless you genuinely expect someone to want to catch SubClassError but not other ValueErrors.

With that said, as you've mentioned, the Python built-in library does subclass ValueError in some cases (e.g. UnicodeError).

like image 181
phooji Avatar answered Oct 13 '22 20:10

phooji


ValueError seems to cover your situation quite well.

like image 1
Ned Batchelder Avatar answered Oct 13 '22 20:10

Ned Batchelder