How to throw KeyNotFoundException from a Dictionary in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
               Dictionary<string, int> dic = new Dictionary<string, int>();

               dic.Add("c#", 100);

               int value = dic["c++"];
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}


/*
run:
     
System.Collections.Generic.KeyNotFoundException: The given key was not present i
n the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at ConsoleApplication_C_Sharp.Program.Main(String[] args) in d:\Conso
leApplication_C_Sharp\ConsoleApplication_C_Sharp\Program.cs:line 16
 
*/

 



answered Feb 21, 2017 by avibootz

Related questions

1 answer 168 views
1 answer 105 views
105 views asked Nov 28, 2020 by avibootz
1 answer 148 views
148 views asked Nov 27, 2020 by avibootz
1 answer 161 views
161 views asked Nov 27, 2020 by avibootz
2 answers 211 views
211 views asked Nov 22, 2020 by avibootz
4 answers 354 views
...