I'm facing a similar problem. I think the ValueError
is better suited for this.
https://docs.python.org/3/library/exceptions.html#ValueError
exception ValueError
: Raised when an 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.
I have a function that can receive two arguments, but should receive only either one of the two, but not both. If both are set, or none of them are set, it's a problem. I use the ValueError
exception for this.
Example code:
def parse_article(self, url: string = None, file: string = None) -> Article:
if url == None and file == None:
raise ValueError("Function was called without any arguments. Please set either url or file, but not both.")
else:
if url != None and file != None: raise ValueError(
"Both url and file were given. Please give either url or file, but not both.")
# Rest of the function.
# Parse the article at the url or in the file, then return it.