Exception for invalid pair of arguments
Asked Answered
S

2

8

Which exception should be thrown by a method that accepts multiple arguments when the arguments given are invalid when given together?

As an example, consider

public Bar DoSomething(Foo f1, Foo f2)
{
  //implementation...
}

where completing the operation is contingent upon some relationship or similarity between f1 and f2 (if arrays/collections, they must be the same size; if athletes, they must be on the same team/opposing teams; etc.).

Each argument is a valid argument for the operation, but they are not valid together. Example:

public MatchResult PlayMatch(Player a, Player b)
{
  if(a.Team == b.Team)
  {
    //Throw exception here, since players must be on different teams
  }

  //Play match, determine winner
}

Throwing an ArgumentException seems incorrect, since it implies that one of the arguments is invalid rather than that the pair of arguments together is invalid.

Scream answered 7/2, 2014 at 18:9 Comment(2)
Can MatchResult not hold a value indicating that the object is invalid?Eliza
Why ArgumentException is invalid? Argument "b.Team" should not be equal to "a.Team"; that's why "b" is an invalid argument.Prussianism
P
2

You don't have to necessary throw an exception. In my opinion, this even should not throw an exception because is a deep part of your matching logic. I would just let matching fail (return zero or whatever you do) instead and mentioned this fact in documentation - something like "Compares two players from different teams." which implies that comparing players from the same team will result in fail.

Pliocene answered 7/2, 2014 at 18:18 Comment(4)
AGREED here. this is NOT an exception rule but a logic rule. never use exceptions to flow your application logic. Always try to determine your inputs as early as possible and show errors there and then if possible.Kiva
Methods and return values should be designed so that problem conditions should only result in exceptions when the caller is not prepared to handle them. If a caller isn't going to be able to handle a condition, having it thrown within the method will avoid the need to clutter up caller with code to test for it. The try/do pattern would be great except for the code duplication it entails; I wish there were a standard convention for a non-redundant try/do pattern.Broder
@Ahmedilyas yet, it helps to track down flaws in the logic or bugs early in development.Damaging
not quite with exceptions and using it as flow logicKiva
G
2

ArgumentException would make sense here, as there is something wrong with the arguments being passed in. You can always add a message to your throw statement:

throw new ArgumentException("Players can't be on the same team!");

It's probably not a good idea to throw an exception here if the user did something wrong. I personally think exceptions should actually catch cases a user can't control (like a file read failing halfway) so you can fail the program gracefully. You can do is just display some sort of error message, return null, and force the user to pick teams again:

public MatchResult PlayMatch(Player a, Player b)
{
    if(a.Team == b.Team)
    {
        MessageBox.Show("Players must be on different teams!");
        return null;
    }

  //Play match, determine winner
}
Gigi answered 7/2, 2014 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.