print - python3 raise exception
Which exception should I raise on bad/illegal argument combinations in Python? (4)
I was wondering about the best practices for indicating invalid argument combinations in Python. I've come across a few situations where you have a function like so:
def import_to_orm(name, save=False, recurse=False): """ :param name: Name of some external entity to import. :param save: Save the ORM object before returning. :param recurse: Attempt to import associated objects as well. Because you need the original object to have a key to relate to, save must be `True` for recurse to be `True`. :raise BadValueError: If `recurse and not save`. :return: The ORM object. """ pass
The only annoyance with this is that every package has its own, usually slightly differing
BadValueError. I know that in Java there exists
java.lang.IllegalArgumentException -- is it well understood that everybody will be creating their own
BadValueErrors in Python or is there another, preferred method?
Agree with Markus' suggestion to roll your own exception, but the text of the exception should clarify that the problem is in the argument list, not the individual argument values. I'd propose:
class BadCallError(ValueError): pass
Used when keyword arguments are missing that were required for the specific call, or argument values are individually valid but inconsistent with each other.
ValueError would still be right when a specific argument is right type but out of range.
Shouldn't this be a standard exception in Python?
In general, I'd like Python style to be a bit sharper in distinguishing bad inputs to a function (caller's fault) from bad results within the function (my fault). So there might also be a BadArgumentError to distinguish value errors in arguments from value errors in locals.
I think the best way to handle this is the way python itself handles it. Python raises a TypeError. For example:
$ python -c 'print(sum())' Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: sum expected at least 1 arguments, got 0
Our junior dev just found this page in a google search for "python exception wrong arguments" and I'm surprised that the obvious (to me) answer wasn't ever suggested in the decade since this question was asked.
I would just raise ValueError, unless you need a more specific exception..
def import_to_orm(name, save=False, recurse=False): if recurse and not save: raise ValueError("save must be True if recurse is True")
There's really no point in doing
class BadValueError(ValueError):pass - your custom class is identical in use to ValueError, so why not use that?
I'm not sure I agree with inheritance from
ValueError -- my interpretation of the documentation is that
ValueError is only supposed to be raised by builtins... inheriting from it or raising it yourself seems incorrect.
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.