I suppose this question is valid for any programming language that can handle (or at least throw) an exception
. However I will stick to Python as an example.
The top 2 answers to the following question disagree with each other on a particular point. https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python
At this moment (2015 March 14 Sat 2354 hrs) it seems that there's a growing consensus around the second answer (Aaron Hall's answer in case any of this changes in the future). And I think I do get the point about being specific about the type of exception I am supposed to raise. A hierarchy for Python is given here: https://docs.python.org/3/library/exceptions.html#exception-hierarchy
Now, I am at point where I want to manually raise an exception when a division results in a non-integer. Specifically, I have a 2D array and I also have the number of columns in it. If the division of the former by the latter results in an non-integer, it means that the number of rows is a non-integer, and thus something's messed up somewhere. And I want to raise an exception
.
Why would it be wrong to just include raise Exception("Something's wrong. Number of rows is not an integer!")
in my code? The categories in the hierarchy are just words in English, but I am not able to choose the best category for my purpose. So what's wrong in clearly explaining the issue in my message (which for the present example is the problem that the number of rows is not an integer)?
ValueError
, as well as giving it a specific human-readable message.ValueError
? I was conflicted betweenArithmeticError
andValueError
when I went through the hierarchy.