C# में KeyNotFoundException तब फेंका जाता है जब आप किसी Dictionary<TKey, TValue> में ऐसी कुंजी (key) से मान प्राप्त करने का प्रयास करते हैं जो डिक्शनरी में मौजूद नहीं है।
उदाहरण: अपवाद उत्पन्न करना
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
try
{
var dictionary = new Dictionary()
{
{ "TV", "Electronics" },
{ "Laptop", "Computers" }
};
// मौजूद नहीं होने वाली कुंजी एक्सेस करना
Console.WriteLine(dictionary["Pen Drive"]);
}
catch (KeyNotFoundException ex)
{
Console.WriteLine("KeyNotFoundException पकड़ा गया!");
Console.WriteLine($"संदेश: {ex.Message}");
Console.WriteLine($"स्टैक ट्रेस: {ex.StackTrace}");
}
catch (Exception ex)
{
Console.WriteLine($"अन्य अपवाद: {ex.GetType().Name} - {ex.Message}");
}
}
}
आउटपुट
KeyNotFoundException पकड़ा गया! संदेश: The given key was not present in the dictionary. स्टैक ट्रेस: at System.Collections.Generic.Dictionary`2.get_Item(TKey key)...
सुरक्षित तरीके: कुंजी मौजूद है या नहीं जांचें
1. ContainsKey() मेथड का उपयोग
if (dictionary.ContainsKey("Pen Drive"))
{
Console.WriteLine(dictionary["Pen Drive"]);
}
else
{
Console.WriteLine("कुंजी नहीं मिली।");
}
2. TryGetValue() मेथड का उपयोग (अनुशंसित)
if (dictionary.TryGetValue("Pen Drive", out string value))
{
Console.WriteLine($"मान मिला: {value}");
}
else
{
Console.WriteLine("कुंजी नहीं मिली।");
}
3. null-कोलेसिंग ऑपरेटर के साथ (C# 7.0+)
string value = dictionary.GetValueOrDefault("Pen Drive", "डिफॉल्ट मान");
Console.WriteLine(value);
पूर्ण तुलनात्मक उदाहरण
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
var dict = new Dictionary()
{
{ "TV", 1 },
{ "Laptop", 2 },
{ "Phone", 3 }
};
string keyToFind = "Tablet";
// तरीका 1: ContainsKey + इंडेक्सर
Console.WriteLine("=== तरीका 1: ContainsKey ===");
if (dict.ContainsKey(keyToFind))
Console.WriteLine(dict[keyToFind]);
else
Console.WriteLine("नहीं मिला");
// तरीका 2: TryGetValue (सबसे कुशल)
Console.WriteLine("\n=== तरीका 2: TryGetValue ===");
if (dict.TryGetValue(keyToFind, out int value))
Console.WriteLine(value);
else
Console.WriteLine("नहीं मिला");
// तरीका 3: GetValueOrDefault (C# 7.0+)
Console.WriteLine("\n=== तरीका 3: GetValueOrDefault ===");
Console.WriteLine(dict.GetValueOrDefault(keyToFind, -1));
// तरीका 4: try-catch (प्रदर्शन के लिए अनुशंसित नहीं)
Console.WriteLine("\n=== तरीका 4: try-catch ===");
try
{
Console.WriteLine(dict[keyToFind]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("नहीं मिला (अपवाद पकड़ा गया)");
}
}
}
आउटपुट
=== तरीका 1: ContainsKey === नहीं मिला === तरीका 2: TryGetValue === नहीं मिला === तरीका 3: GetValueOrDefault === -1 === तरीका 4: try-catch === नहीं मिला (अपवाद पकड़ा गया)
महत्वपूर्ण बिंदु
- TryGetValue() सबसे कुशल है - एक ही लुकअप में जांच और मान प्राप्ति दोनों करता है
- ContainsKey() + इंडेक्सर दो लुकअप करता है (पहले जांच, फिर एक्सेस)
- try-catch का उपयोग केवल असाधारण स्थितियों के लिए करें, सामान्य फ्लो कंट्रोल के लिए नहीं
GetValueOrDefault()(C# 7.0+) डिफॉल्ट मान प्रदान करने का साफ तरीका है- प्रदर्शन-महत्वपूर्ण कोड में अपवाद-आधारित दृष्टिकोण से बचें