I have this code:
else if (number == 5)
{
Console.Write("Student's index: ");
int index1 = int.Parse(Console.ReadLine());
try
{
customDataList.FindStudent(index1); //displays the element that has the specified index
}
catch (ArgumentOutOfRangeException)
{
Console.WriteLine("Please choose an index from 0 to 9!");
}
}
I need to handle errors using try-catch when the user doesn't enter any character or enters a non-integer character. How can that be done?
int.TryParsehere? See learn.microsoft.com/en-us/dotnet/api/system.int32.tryparseArgumentNullExceptionand/orFormatExceptionin separatecatchblocks. Or, a better approach, follow the ideas on the comments above and useint.TryParseto validated the input before trying to use it.