Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 217467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:39:36+00:00 2026-05-11T18:39:36+00:00

When you try to access a key which isn’t in a Dictionary (for example),

  • 0

When you try to access a key which isn’t in a Dictionary (for example), here’s the stack trace you get :

   at System.ThrowHelper.ThrowKeyNotFoundException()
     at System.Collections.Generic.Dictionary`2.get_Item(TKey key)   
       .... .... (my own code stack trace)

Like most people probably do, I log this errors when they occur, and try to figure out what happened.

The two key informations I want are where did this occur (the stacktrace is very helpful for that), and the key which caused the exception, which doesn’t appear anywhere.

Either I didn’t look properly (the KeyNotFoundException class contains a “Data” member, which is always empty, and the “Message” field” doesn’t contain the key value), or it wasn’t included in the framework at all.

I can’t imagine nobody in the .net BCL team thought this would be an useful feature to have. I’m curious to know why they didn’t include it. Are there some good reasons not to?

How do you deal with those exceptions ? The only alternative I can think of is to use a custom extension method on Dictionary which would wrap the call, catch the exception, and rethrow it with additional information regarding the key, but that wouldn’t help for code I don’t own, and it feel broken to change such a “base” functionality.

What do you think ?


I’m aware of this question, regarding the general fact that one cannot access the arguments of the method which raised an exception. My question is specifically related to the KeyNotFoundException.


Edit : I’m aware of the TryGetValue pattern, I do that all the time when I expect that my collection may not contain the key. But when it should contain it, I don’t test, because I prefer my program to fail with an exception so that I know something unexpected happened before (ie at the time when the key should have been inserted)

I could wrap a try/catch/log error/rethrow around all my dictionary access, but this would lead to difficult-to-read code, cluterred with all my exception handling/logging stuff.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:39:37+00:00Added an answer on May 11, 2026 at 6:39 pm

    Why do you rely on (slow) exceptions? Just verify whether the key exists with ContainsKey or TryGetValue?

    I don’t know the reason why the exception doesn’t contain the error-causing field (maybe because it should be ungeneric) but just wrap it if you think you’ll need it.

    class ParameterizedKeyNotFoundException<T> : KeyNotFoundException {
        public T InvalidKey { get; private set; }
    
        public ParameterizedKeyNotFoundException(T InvalidKey) {
            this.InvalidKey = InvalidKey;
        }
    }
    
    static class Program {
    
        static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> Dict, TKey Key) {
            TValue res;
    
            if (Dict.TryGetValue(Key, out res))
                return res;
    
            throw new ParameterizedKeyNotFoundException<TKey>(Key);
        }
    
        static void Main(string[] args) {
    
            var x = new Dictionary<string, int>();
    
            x.Add("foo", 42);
    
            try {
                Console.WriteLine(x.Get("foo"));
                Console.WriteLine(x.Get("bar"));
            }
            catch (ParameterizedKeyNotFoundException<string> e) {
                Console.WriteLine("Invalid key: {0}", e.InvalidKey);
            }
    
            Console.ReadKey();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a pretty good description of how to do… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Have you overridden the loadView method in PageViewController? What if… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer If you are looking at a particular row, you can… May 11, 2026 at 11:55 pm

Related Questions

When you try to access a key which isn't in a Dictionary (for example),
I'm a freshman in college going for my computer science degree... I've programmed plenty
I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I
I am writing my first FireFox extension and I have some questions. Maybe someone
I have the following situation: I built an Access form with a subform (which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.