Advertisement
PawsonCz

try...catch

Mar 7th, 2021
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. public class Program
  5. {
  6.  
  7.     public static void Main()
  8.     {
  9.  
  10.         string name;
  11.         int[] Tablica = new int[5];
  12.         Console.Write("Podaj swoje imię\t");
  13.         name = Console.ReadLine();
  14.  
  15.        
  16.         try
  17.         {
  18.             Console.Write("Podaj element tablicy: \t");
  19.             Tablica[5] = int.Parse(Console.ReadLine());
  20.             Console.WriteLine($"Piąty element tablicy ma wartość {Tablica[5]}");
  21.         }
  22.         catch(IndexOutOfRangeException e)
  23.         {
  24.             Console.WriteLine($"{name} Indeks tablicy liczony jest od zera.\nPróbujesz przypisać wartość do nieistniejącego elementu.\nSpróbuj ponownie");
  25.             Tablica[4] = int.Parse(Console.ReadLine());
  26.             Console.WriteLine(e.Message);
  27.         }
  28.  
  29.  
  30.     }
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement