I am trying to find out the advantages and disadvantages for a method with multiple result values.
For example I'm using a login-method. If the login was successful, it will pass, otherwise I need to know why it failed.
1. Return true or false (Not enough information)
bool Login(string user, string password);
2. Return true, if it was successful, otherwise throw an exception
public class UnknownUserException : Exception { }
public class WrongPasswordException : Exception { }
bool Login(string user, string password);
3. Return nothing. Throw an exception, if it wasn't successful
public class UnknownUserException : Exception { }
public class WrongPasswordException : Exception { }
void Login(string user, string password);
4. Return an enum value
enum LoginResult
{
Successful
UnknownUser,
WrongPassword
}
LoginResult Login(string user, string password);
"Login" is just one example case. I would like to know what the advantages and disadvantages of the different implementations is, and for which cases they are more or less appropriate.