As the name implies, ArgumentException is an exception to the argument. This means that the argument was somehow wrong.
General form:
public void SomeMethod(SomeType arg) { if(!TestArgValid(arg)) throw new ArgumentException("arg");
If the only possible way that GetUserById could fail was that something with the idOfUser value was essentially wrong, then the following would be the same in practice:
public void UpdateUser(int idOfUser) { if(!TestValid(idOfUser)) throw new ArgumentException("idOfUser"); var user = GetUserById(idOfUser);
And , if for some reason it turned out to be faster or less wasteful due to some resource for testing user after the fact than idOfUser before the fact and if > there were no side effects of calling GetUserById , and if the difference mattered, the second version is possible would be a reasonable optimization of the first.
But this is only true if all of the above ifs characters are saved, and then this is a strange way to detect an invalid argument, which has a definite advantage when we benefit from encapsulating methods, hiding this oddity from everything else.
Most likely, there may be a valid idOfUser for which there was no corresponding user , in which case, of course, it was no exception.
Jon hanna
source share