Hello. I never used exceptions, but now I decided to clarify this topic for myself. I found it pleasant to use exceptions, but here I also saw a warning against this. Imagine a case where we have a method such as user registration. This method hides in itself all the difficulties of registration, for example, verification of login and email. If the login is busy, the message should be displayed, the same with the email. The method itself is called from the asp.net controller.
public void RegisterUser(SignUpModel registerData) { if(!UserData.UserEmailIsFree(registerData.Email)) { throw new EmailIsNotFreeException(); } if(!UserData.UserLoginIsFree(registerData.Login)) { throw new LoginIsNotFreeException(); } User newUser = new User(); newUser.Login = registerData.Login; newUser.Password = registerData.Password; newUser.Email = registerData.Email; UserData.AddUser(newUser); }
It would look like a code with exceptions. I understand this is a bad approach, but it's pretty darn convenient. In another case, return codes should be applied, as far as I understand. It turns out a vicious circle. Explain to me what to do? I need authoritative sources, code samples, elegant solutions and everything that you can help me. I understand that there are a lot of holivars on the Internet on this subject, but nowhere does it say which way to go without using exceptions. A lot of people say that exceptions are bad everywhere, but how to be then. I will be glad to each more or less useful answer. Thank!