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 6108247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:16:33+00:00 2026-05-23T14:16:33+00:00

I have an C# Silverlight application that is randomly throwing a KeyNotFoundException . I

  • 0

I have an C# Silverlight application that is randomly throwing a “KeyNotFoundException“. I have no idea what key cannot be found. This has lead me to two questions:

  1. Does a KeyNotFoundException store/expose what key it tried to find? When I looked in the documentation, I did not see anything that implied this information was available.
  2. I am catching/logging this exception in a general Application.UnhandledException event handler. My question is, if I catch the event here, can I convert the ExceptionObject to a KeyNotFoundException and still get the key information if its exposed as asked in #1?

Thank you so much for your help!

  • 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-23T14:16:34+00:00Added an answer on May 23, 2026 at 2:16 pm

    A KeyNotFoundException is caused by trying to get the value out of a Dictionary with a given key when they key is not present. For example:

    var dictionary = new Dictionary<string, string>();
    var val = dictionary["mykey"];
    

    You can look at all of the places where a dictionary is being used and determine yourself. A general best practice for that is if you are looking for a value in a dictionary that might not be present would be to use TryGetValue. Catching the exception every time is a more expensive operation and is unnecessary:

    string val;
    if(dictionary.TryGetValue("mykey", out val))
    {
         //The key was found. The value is in val.
    }
    else
    {
        //The key was not present.
    }
    

    You can look at the StackTrace property of they KeyNotFoundException to determine exactly where the problem is happening. All exceptions have the StackTrace property, so you don’t need to care what type of exception it was in your global error handler. For example:

    private void Application_UnhandledException(object sender, 
            ApplicationUnhandledExceptionEventArgs e)
        {
             var stackTrace = e.ExceptionObject.StackTrace;
             //Log the stackTrace somewhere.
        }
    

    Or, if you want to be able to tell what type of Exception it is:

    private void Application_UnhandledException(object sender, 
            ApplicationUnhandledExceptionEventArgs e)
        {
             if (e.ExceptionObject is KeyNotFoundException)
             {
                 //This was a KeyNotFoundException
             }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a silverlight application that uses wcf service. This application is shown from
I have a Silverlight application that is interacting with a WCF service. This service
I have a Silverlight application that is building a URL. This URL is a
I have a Silverlight application that has a ListBox of a specific width. I
I have a Silverlight application that I am trying to style. This style simply
I have a Silverlight application that has a DataGrid and a DataPager. The data
I have a Silverlight application that has a DataGrid. I need to print the
I have a Silverlight application that interfaces with an SQL database. It does this
I have a Silverlight 2 application that has a Loading Data... message that contains
I have a Silverlight application that has three regions that will host the same

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.