Генерация собственного исключения - C#

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

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

Попытался кинуть исключение, не получилось. Хочу : Чтобы при вводе варианта (iNoVar) кроме 1го, выводило исключение. Вот написал, но машина ругается: 1) Обнаружен недостижимый код 2) Не все ветви кода возвращают значение
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Lab2
  7. {
  8. class Program
  9. {
  10. static int EnterVar()
  11. {
  12. try
  13. {
  14. Console.Write("Enter Variation :");
  15. int iNoVar = int.Parse(Console.ReadLine());
  16. switch (iNoVar)
  17. {
  18. case 1: return (iNoVar);
  19. default: iNoVar = 0;
  20. throw new Exception("Incorrect input variation");
  21. }
  22. }
  23. catch (Exception e)
  24. {
  25. Console.WriteLine("Exception in Enter variation :" + e.Message);
  26. }
  27. }
  28. static void Main(string[] args)
  29. {
  30. int iNoVarMain = EnterVar();
  31. Console.WriteLine("Entered Variation: {0} ", iNoVarMain);
  32. Console.ReadLine();
  33. }
  34. }
  35. }

Решение задачи: «Генерация собственного исключения»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     internal class Program
  10.     {
  11.         private static int EnterVar()
  12.         {
  13.             Console.Write("Enter Variation :");
  14.             int iNoVar = int.Parse(Console.ReadLine());
  15.             switch (iNoVar)
  16.             {
  17.                 case 1:
  18.                     return (iNoVar);
  19.                 default:
  20.                     throw new ArgumentException();
  21.             }
  22.         }
  23.  
  24.         private static void Main(string[] args)
  25.         {
  26.             try
  27.             {
  28.                 int iNoVarMain = EnterVar();
  29.                 Console.WriteLine("Entered Variation: {0} ", iNoVarMain);
  30.             }
  31.             catch (ArgumentException ex)
  32.             {
  33.                 Console.WriteLine("Exception in Enter variation : Incorrect input variation");
  34.             }
  35.             catch (Exception ex)
  36.             {
  37.                 Console.Write(ex.Message);
  38.             }
  39.  
  40.             Console.ReadLine();
  41.         }
  42.     }
  43. }

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


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

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

6   голосов , оценка 4.167 из 5

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

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

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