The loop accepts user input into the string and parses it, in case of unsuccessful parsing or request to the server via ScreenName - returns an Exception. What condition can be put to the cycle so that it repeats until the user enters the correct value and the method does not return the correct answer?
int? numberId; while (true) { Console.WriteLine("Введите Id, либо ScreenName группы"); var enterId = Console.ReadLine(); numberId = taskLogic.IdTreatment(enterId); if(numberId != null) break; } public int? IdTreatment(string enterId) { if (int.TryParse(enterId, out int numberId)) { Console.WriteLine("Id принят успешно"); return (int?)numberId; } else { Console.WriteLine("Обнаружен ScreenName"); numberId = (int)api.Utils.ResolveScreenName(enterId).Id.Value; return (int?)numberId; } }
if(numberId == null)does not suit you? If you need to handle Exception, then try-catch with a null return. This is a very bad practice, no need to build the logic of the program on exceptions. But if you don’t have a choice, why is this option not suitable for you? - Lunar Whisper