Я пишу программу, где мне нужно написать уловить нулевые или общие исключения ввода с помощью try ... catch блоков и убедиться, что вход находится между двумя номерами с if заявление.Исключение, проигнорированное (приложение формы C#)
Моя проблема заключается в том, что исключение игнорируется, когда я помещаю нуль, строку или другой символ, он просто переходит к выражению if. Если я прокомментирую инструкцию if, исключение все равно не поймается.
const char DELIM = ',';
int input;
const int MIN = 1;
const int MAX = 10;
//convert the input to an integer
int.TryParse(tbInput.Text, out input);
//check if the user has selected enter yet
if (e.KeyCode == Keys.Enter)
{
try
{
//if the number entered is out of ranger, show error
if (input < MIN || input > MAX)
{
MessageBox.Show("Please enter an integer between 1 and 10");
}
else
{
//if the number is in range, write to file
StreamWriter writer = new StreamWriter(file);
writer.WriteLine(input.ToString() + DELIM + " ");
writer.Flush();
//flush the information to the file each time
}
}
// any exceptions that occur will be caught here
catch (IOException)
{
MessageBox.Show("Error with input");
}
finally
{
//clear the taxtbox for the next entry
tbInput.Clear();
}
}
Вот мой код. Если кто-то может указать, где я что-то упускаю, или что я должен изменить, я бы очень признателен. Приветствия!
В какой строке вы ожидаете выбросить ошибку? –