Генерация собственного исключения - C#
Формулировка задачи:
Попытался кинуть исключение, не получилось. Хочу : Чтобы при вводе варианта (iNoVar) кроме 1го, выводило исключение. Вот написал, но машина ругается:
1) Обнаружен недостижимый код
2) Не все ветви кода возвращают значение
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab2
- {
- class Program
- {
- static int EnterVar()
- {
- try
- {
- Console.Write("Enter Variation :");
- int iNoVar = int.Parse(Console.ReadLine());
- switch (iNoVar)
- {
- case 1: return (iNoVar);
- default: iNoVar = 0;
- throw new Exception("Incorrect input variation");
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception in Enter variation :" + e.Message);
- }
- }
- static void Main(string[] args)
- {
- int iNoVarMain = EnterVar();
- Console.WriteLine("Entered Variation: {0} ", iNoVarMain);
- Console.ReadLine();
- }
- }
- }
Решение задачи: «Генерация собственного исключения»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- internal class Program
- {
- private static int EnterVar()
- {
- Console.Write("Enter Variation :");
- int iNoVar = int.Parse(Console.ReadLine());
- switch (iNoVar)
- {
- case 1:
- return (iNoVar);
- default:
- throw new ArgumentException();
- }
- }
- private static void Main(string[] args)
- {
- try
- {
- int iNoVarMain = EnterVar();
- Console.WriteLine("Entered Variation: {0} ", iNoVarMain);
- }
- catch (ArgumentException ex)
- {
- Console.WriteLine("Exception in Enter variation : Incorrect input variation");
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message);
- }
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д