Обработка некорректного ввода, используя механизм исключений и блок Try-Catch - C#

Узнай цену своей работы

Формулировка задачи:

Есть код Нужно сделать так, что бы после некорректного ввода(например System.FormatException или System.OverflowException и тд ) пользователя программа делала перезапрос у пользователя. Вроде можно это сделать с помощью try{} catch { }. Как это делать ? Помогите пожалуйста
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication14
  7. { class Program
  8. { static void Main(string[] args)
  9. {
  10. Byte b1, b2; byte b3;
  11. Console.WriteLine("Введите число с диапазоном От 0 до 255");
  12. b1 = Convert.ToByte(Console.ReadLine());
  13. Console.WriteLine("Введите число с диапазоном От 0 до 255");
  14. b2 = Byte.Parse(Console.ReadLine());
  15. Console.WriteLine("Введите число с диапазоном От 0 до 255");
  16. Byte.TryParse(Console.ReadLine(), out b3);
  17. SByte sb1, sb2; sbyte sb3;
  18. Console.WriteLine("Введите число с диапазоном От -128 до 127");
  19. sb1 = Convert.ToSByte(Console.ReadLine());
  20. Console.WriteLine("Введите число с диапазоном От -128 до 127");
  21. sb2 = SByte.Parse(Console.ReadLine());
  22. Console.WriteLine("Введите число с диапазоном От -128 до 127");
  23. SByte.TryParse(Console.ReadLine(), out sb3);
  24. Int16 sh1, sh2; short sh3;
  25. Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
  26. sh1 = Convert.ToInt16(Console.ReadLine());
  27. Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
  28. sh2 = Int16.Parse(Console.ReadLine());
  29. Console.WriteLine("Введите число с диапазоном От -32 768 до 32 767");
  30. Int16.TryParse(Console.ReadLine(), out sh3);
  31. UInt16 ush1, ush2; ushort ush3;
  32. Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
  33. ush1 = Convert.ToUInt16(Console.ReadLine());
  34. Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
  35. ush2 = UInt16.Parse(Console.ReadLine());
  36. Console.WriteLine("Введите число с диапазоном От 0 до 65 535");
  37. UInt16.TryParse(Console.ReadLine(), out ush3);
  38. Int32 i1, i2;int i3;
  39. Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
  40. i1 = Convert.ToInt32(Console.ReadLine());
  41. Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
  42. i2 = Int32.Parse(Console.ReadLine());
  43. Console.WriteLine("Введите число с диапазоном От -2 147 483 648 до 2 147 483 647");
  44. Int32.TryParse(Console.ReadLine(), out i3);
  45. UInt32 ui1, ui2; uint ui3;
  46. Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
  47. ui1 = Convert.ToUInt32(Console.ReadLine());
  48. Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
  49. ui2 = UInt32.Parse(Console.ReadLine());
  50. Console.WriteLine("Введите число с диапазоном От 0 до 4 294 967 295");
  51. UInt32.TryParse(Console.ReadLine(), out ui3);
  52. Int64 l1, l2; long l3;
  53. Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
  54. l1 = Int64.Parse(Console.ReadLine());
  55. Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
  56. l2 = Convert.ToInt64(Console.ReadLine());
  57. Console.WriteLine("Введите число с диапазоном От -922 337 203 685 477 508 до 922 337 203 685 477 507");
  58. Int64.TryParse(Console.ReadLine(), out l3);
  59. UInt64 ul1, ul2; ulong ul3;
  60. Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
  61. ul1 = UInt64.Parse(Console.ReadLine());
  62. Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
  63. ul2 = Convert.ToUInt64(Console.ReadLine());
  64. Console.WriteLine("Введите число с диапазоном От 0 до 18 446 744 073 709 551 615");
  65. UInt64.TryParse(Console.ReadLine(), out ul3);
  66. Single f1, f2; float f3;
  67. Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
  68. f1 = Single.Parse(Console.ReadLine());
  69. Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
  70. f2 = Convert.ToSingle(Console.ReadLine());
  71. Console.WriteLine("Введите число с диапазоном От -3,402 823e38 до 3,402 823e38");
  72. Single.TryParse(Console.ReadLine(), out f3);
  73. Double d1, d2;double d3;
  74. Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
  75. d1 = Double.Parse(Console.ReadLine());
  76. Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
  77. d2 = Convert.ToDouble(Console.ReadLine());
  78. Console.WriteLine("Введите число с диапазоном От -1,797 693 134 862 32e308 до 1,797 693 134 862 32e308");
  79. Double.TryParse(Console.ReadLine(), out d3);
  80. Char c1, c2; char c3;
  81. Console.WriteLine("Введите символ");
  82. c1 = Char.Parse(Console.ReadLine());
  83. Console.WriteLine("Введите символ");
  84. c2 = Convert.ToChar(Console.ReadLine());
  85. Console.WriteLine("Введите символ");
  86. Char.TryParse(Console.ReadLine(), out c3);
  87. string s;
  88. Console.WriteLine("Введите строку");
  89. s = Convert.ToString(Console.ReadLine());
  90. //Вывод
  91. Console.Clear();
  92. Console.Write("ТИП ДАННЫХ | Convert | Parse | TryParse |");
  93. Console.SetCursorPosition(0, 1);
  94. Console.Write("-----------------------------------------------------------------------------|");
  95. Console.SetCursorPosition(0, 2);
  96. 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("|");
  97. Console.SetCursorPosition(0, 3);
  98. 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("|");
  99. Console.SetCursorPosition(0, 4);
  100. 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("|");
  101. Console.SetCursorPosition(0, 5);
  102. 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("|");
  103. Console.SetCursorPosition(0, 6);
  104. 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("|");
  105. Console.SetCursorPosition(0, 7);
  106. 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("|");
  107. Console.SetCursorPosition(0, 8);
  108. 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("|");
  109. Console.SetCursorPosition(0, 9);
  110. 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("|");
  111. Console.SetCursorPosition(0, 10);
  112. 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("|");
  113. Console.SetCursorPosition(0, 11);
  114. 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("|");
  115. Console.SetCursorPosition(0, 12);
  116. 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("|");
  117. Console.SetCursorPosition(0, 13);
  118. 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("|");
  119. Console.SetCursorPosition(0, 14);
  120. Console.Write("-----------------------------------------------------------------------------|");
  121. Console.SetCursorPosition(27, 16);
  122. Console.ForegroundColor = ConsoleColor.DarkGreen;
  123. Console.Write("ВРЕМЯ:{0}", DateTime.Now);
  124. Console.ReadKey();
  125. }
  126. }
  127. }

Решение задачи: «Обработка некорректного ввода, используя механизм исключений и блок Try-Catch»

textual
Листинг программы
  1. private byte InputByte()
  2. {
  3. try
  4. {
  5. Console.WriteLine("Введите число с диапазоном От 0 до 255");
  6. b = Convert.ToByte(Console.ReadLine());
  7. return b;
  8. }
  9. catch (Exception e)
  10. {
  11. Console.WriteLine("Ввод некорректен: "+e.Message);
  12. return InputByte();
  13. }
  14. }
  15.  
  16. static void Main(string[] args)    
  17. {        
  18. byte[] byteArray = new byte[3];
  19. for(int i=0;i<3;i++)
  20. byteArray[i] = InputByte();
  21. ...
  22. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 3.857 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы