Обработка некорректного ввода, используя механизм исключений и блок Try-Catch - C#
Формулировка задачи:
Есть код
Нужно сделать так, что бы после некорректного ввода(например System.FormatException или System.OverflowException и тд ) пользователя программа делала перезапрос у пользователя.
Вроде можно это сделать с помощью try{} catch { }.
Как это делать ? Помогите пожалуйста
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{ class Program
{ static void Main(string[] args)
{
Byte b1, b2; byte b3;
Console.WriteLine("Введите число с диапазоном От 0 до 255");
b1 = Convert.ToByte(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 255");
b2 = Byte.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 255");
Byte.TryParse(Console.ReadLine(), out b3);
SByte sb1, sb2; sbyte sb3;
Console.WriteLine("Введите число с диапазоном От -128 до 127");
sb1 = Convert.ToSByte(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -128 до 127");
sb2 = SByte.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -128 до 127");
SByte.TryParse(Console.ReadLine(), out sb3);
Int16 sh1, sh2; short sh3;
Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
sh1 = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
sh2 = Int16.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
Int16.TryParse(Console.ReadLine(), out sh3);
UInt16 ush1, ush2; ushort ush3;
Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
ush1 = Convert.ToUInt16(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
ush2 = UInt16.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
UInt16.TryParse(Console.ReadLine(), out ush3);
Int32 i1, i2;int i3;
Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
i1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
i2 = Int32.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
Int32.TryParse(Console.ReadLine(), out i3);
UInt32 ui1, ui2; uint ui3;
Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
ui1 = Convert.ToUInt32(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
ui2 = UInt32.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
UInt32.TryParse(Console.ReadLine(), out ui3);
Int64 l1, l2; long l3;
Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
l1 = Int64.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
l2 = Convert.ToInt64(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
Int64.TryParse(Console.ReadLine(), out l3);
UInt64 ul1, ul2; ulong ul3;
Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
ul1 = UInt64.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
ul2 = Convert.ToUInt64(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
UInt64.TryParse(Console.ReadLine(), out ul3);
Single f1, f2; float f3;
Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
f1 = Single.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
f2 = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
Single.TryParse(Console.ReadLine(), out f3);
Double d1, d2;double d3;
Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
d1 = Double.Parse(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
d2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
Double.TryParse(Console.ReadLine(), out d3);
Char c1, c2; char c3;
Console.WriteLine("Введите символ");
c1 = Char.Parse(Console.ReadLine());
Console.WriteLine("Введите символ");
c2 = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Введите символ");
Char.TryParse(Console.ReadLine(), out c3);
string s;
Console.WriteLine("Введите строку");
s = Convert.ToString(Console.ReadLine());
//Вывод
Console.Clear();
Console.Write("ТИП ДАННЫХ | Convert | Parse | TryParse |");
Console.SetCursorPosition(0, 1);
Console.Write("-----------------------------------------------------------------------------|");
Console.SetCursorPosition(0, 2);
Console.Write("Byte"); Console.SetCursorPosition(18, 2); Console.Write("|" + "{0:x}", b1); Console.SetCursorPosition(37, 2); Console.Write("|" + "{0:x}", b2); Console.SetCursorPosition(55, 2); Console.Write("|" + "{0:x}", b3); Console.SetCursorPosition(77, 2); Console.Write("|");
Console.SetCursorPosition(0, 3);
Console.Write("Sbyte"); Console.SetCursorPosition(18, 3); Console.Write("|" + "{0:d}", sb1); Console.SetCursorPosition(37, 3); Console.Write("|" + "{0:d}", sb2); Console.SetCursorPosition(55, 3); Console.Write("|" + "{0:d}", sb3); Console.SetCursorPosition(77, 3); Console.Write("|");
Console.SetCursorPosition(0, 4);
Console.Write("Int32"); Console.SetCursorPosition(18, 4); Console.Write("|" + "{0:g4}", i1); Console.SetCursorPosition(37, 4); Console.Write("|" + "{0:g4}", i2); Console.SetCursorPosition(55, 4); Console.Write("|" + "{0:g4}", i3); Console.SetCursorPosition(77, 4); Console.Write("|");
Console.SetCursorPosition(0, 5);
Console.Write("Uint32"); Console.SetCursorPosition(18, 5); Console.Write("|" + "{0:d}", ui1); Console.SetCursorPosition(37, 5); Console.Write("|" + "{0:d}", ui2); Console.SetCursorPosition(55, 5); Console.Write("|" + "{0:d}", ui3); Console.SetCursorPosition(77, 5); Console.Write("|");
Console.SetCursorPosition(0, 6);
Console.Write("Int16"); Console.SetCursorPosition(18, 6); Console.Write("|" + "{0:e}", sh1); Console.SetCursorPosition(37, 6); Console.Write("|" + "{0:e}", sh2); Console.SetCursorPosition(55, 6); Console.Write("|" + "{0:e}", sh3); Console.SetCursorPosition(77, 6); Console.Write("|");
Console.SetCursorPosition(0, 7);
Console.Write("UInt16"); Console.SetCursorPosition(18, 7); Console.Write("|" + "{0:f1}", ush1); Console.SetCursorPosition(37, 7); Console.Write("|" + "{0:f1}", ush2); Console.SetCursorPosition(55, 7); Console.Write("|" + "{0:f1}", ush3); Console.SetCursorPosition(77, 7); Console.Write("|");
Console.SetCursorPosition(0, 8);
Console.Write("Int64"); Console.SetCursorPosition(18, 8); Console.Write("|" + "{0:c}", l1); Console.SetCursorPosition(37, 8); Console.Write("|" + "{0:c}", l2); Console.SetCursorPosition(55, 8); Console.Write("|" + "{0:c}", l3); Console.SetCursorPosition(77, 8); Console.Write("|");
Console.SetCursorPosition(0, 9);
Console.Write("UInt64"); Console.SetCursorPosition(18, 9); Console.Write("|" + "{0:d}", ul1); Console.SetCursorPosition(37, 9); Console.Write("|" + "{0:d}", ul2); Console.SetCursorPosition(55, 9); Console.Write("|" + "{0:d}", ul3); Console.SetCursorPosition(77, 9); Console.Write("|");
Console.SetCursorPosition(0, 10);
Console.Write("Single"); Console.SetCursorPosition(18, 10); Console.Write("|" + "{0:n}", f1); Console.SetCursorPosition(37, 10); Console.Write("|" + "{0:n}", f2); Console.SetCursorPosition(55, 10); Console.Write("|" + "{0:n}", f3); Console.SetCursorPosition(77, 10); Console.Write("|");
Console.SetCursorPosition(0, 11);
Console.Write("Double"); Console.SetCursorPosition(18, 11); Console.Write("|" + "{0:p}", d1); Console.SetCursorPosition(37, 11); Console.Write("|" + "{0:p}", d2); Console.SetCursorPosition(55, 11); Console.Write("|" + "{0:p}", d3); Console.SetCursorPosition(77, 11); Console.Write("|");
Console.SetCursorPosition(0, 12);
Console.Write("Char"); Console.SetCursorPosition(18, 12); Console.Write("|" + c1); Console.SetCursorPosition(37, 12); Console.Write("|" + c2); Console.SetCursorPosition(55, 12); Console.Write("|" + c3); Console.SetCursorPosition(77, 12); Console.Write("|");
Console.SetCursorPosition(0, 13);
Console.Write("String"); Console.SetCursorPosition(18, 13); Console.Write("|" + s); Console.SetCursorPosition(37, 13); Console.Write("| -"); Console.SetCursorPosition(55, 13); Console.Write("| -"); Console.SetCursorPosition(77, 13); Console.Write("|");
Console.SetCursorPosition(0, 14);
Console.Write("-----------------------------------------------------------------------------|");
Console.SetCursorPosition(27, 16);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("ВРЕМЯ:{0}", DateTime.Now);
Console.ReadKey();
}
}
}Решение задачи: «Обработка некорректного ввода, используя механизм исключений и блок Try-Catch»
textual
Листинг программы
private byte InputByte()
{
try
{
Console.WriteLine("Введите число с диапазоном От 0 до 255");
b = Convert.ToByte(Console.ReadLine());
return b;
}
catch (Exception e)
{
Console.WriteLine("Ввод некорректен: "+e.Message);
return InputByte();
}
}
static void Main(string[] args)
{
byte[] byteArray = new byte[3];
for(int i=0;i<3;i++)
byteArray[i] = InputByte();
...
}