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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:09:19+00:00 2026-05-14T14:09:19+00:00

I could have sworn that I’ve used NUnit’s Assert.Throws to determine whether or not

  • 0

I could have sworn that I’ve used NUnit’s Assert.Throws to determine whether or not a particular exception gets thrown from a method, but my memory has failed me before. I read this post here on SO, but it didn’t answer my question, as I know the correct syntax, and I don’t want to do anything with the exception that gets returned (I don’t want to look at the Exception’s members, though this could be useful down the road).

I wrote unit tests to prove my lack of understanding in the use of Dictionary, and couldn’t get it handle the KeyNotFoundException that gets thrown. Instead of NUnit catching it and passing the test, I get an unhandled KeyNotFoundException error when I run. I verified that I don’t have the VS IDE set up to break on thrown .NET exceptions.

I’ve tried this two ways:

Assert.Throws( typeof(KeyNotFoundException), () => value = prefs["doesn't exist"]);

and

Assert.Throws<KeyNotFoundException>( () => value = prefs["doesn't exist"]);

but both result in an unhandled exception. What am I missing here?

UPDATE seems like others can’t reproduce this. Here’s a screenshot:

alt text

  • 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-14T14:09:20+00:00Added an answer on May 14, 2026 at 2:09 pm

    The debugger is stating that your exception is not being handled by user code, which is technically true. To demonstrate, I’ll use the sample test sgreeve provided

    [Test]
    public void demonstrateThatExceptionThrown()
    {
        string value;
        Dictionary<string, string> test = new Dictionary<string, string>();
        Assert.Throws(typeof(KeyNotFoundException), () => value = test["h"]);
    }
    

    When you execute it, you will receive a warning in VisualStudio that the exception is unhandled in user code. If you look at the callstack, you will see something like

    [External Code]
    CodeTests.DLL!CodeTests.MiscTests.demonstrateThatExceptionThrown.AnonymousMethod()
    [External Code]
    CodeTests.DLL!CodeTests.MiscTests.demonstrateThatExceptionThrown()
    [External Code]
    

    Because you have specified a delegate, the exception is happening within the “AnonymousMethod” that was created. This is being called by the .Net framework. The debugger is stopping because your delegate isn’t handling the exception before it gets passed back to the framework. It doesn’t care that further up the stack it might be handled in your code (perhaps since there is no way to guarantee that the external code will handle the exception correctly.)

    To have VisualStudio see this as a handled exception, use the ExpectedException attribute and remove the delegate, like so:

    [Test]
    [ExpectedException(typeof(KeyNotFoundException))]
    public void demonstrateThatExceptionThrown()
    {
        string value;
        Dictionary<string, string> test = new Dictionary<string, string>();
        value = test["h"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could have sworn that Crystal Reports used to sort strings beginning with a
I could have sworn that I saw a quick demo of new features in
Maybe I'm going insane, but I could have sworn that there was an PHP
I could have sworn you could do the following: ls *.{java, cpp} but that
I could have sworn that I seen a while back that the max image
I could have sworn that there was an extension method already built for the
I could have sworn i have done this somewhere - im using 2.0 right
I know I could have an attribute but that's more work than I want
I could have sworn I’d read a question about this before, but I can’t
I have a sub report on another report that could have no records in

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.