Python Forum

Full Version: what type of exception to use?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a function that uses some keyword arguments. two of the keyword arguments cannot be used in the same call and if so the function will raise an exception. all of its keyword arguments do take the value None as being the same as that keyword argument not being used at all.

my question is: which exception should be raised in this case?
Depending on the project, I might prefer a custom exception. They're cheap and not going to collide with anything.

But if I needed to use a built in, it seems like you're complaining about the values, so I'd lean toward it being a ValueException.
I'd choose a TypeError or your own subclass of TypeError, because if you change the values of the arguments without changing their type, there is still an error.
any and every case of using foo=notNone and also using bar=notNone needs to raise this exception. if only one of them is used then this conflict is avoided but it could instead have a genuine TypeError or ValueError if the one value passed is a bad type or bad value. it is not essential to have 3 different kinds of exceptions. the function will be explicitly testing for these.