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

  • SEARCH
  • Home
  • 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 3235798
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:32:53+00:00 2026-05-17T17:32:53+00:00

If I want to test that a method throws an exception of a particular

  • 0

If I want to test that a method throws an exception of a particular type, NUnit’s ExpectedException attribute doesn’t care about the actual type; if I throw a generic Exception before the method call, the test passes:

    [Test, ExpectedException(typeof(TestCustomException))]
    public void FirstOnEmptyEnumerable()
    {
        throw new Exception(); // with this, the test should fail, but it doesn't
        this.emptyEnumerable.First(new TestCustomException());
    }

If I want to check that the test throws the exact exception type, I have to do something manual like this:

    [Test]
    public void FirstOnEmptyEnumerable()
    {
        try
        {
            throw new Exception();  // now the test fails correctly.
            this.emptyEnumerable.First(new TestCustomException());
        }
        catch (TestCustomException)
        {
            return;
        }

        Assert.Fail("Exception not thrown.");
    }

Am I missing something?

  • 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-17T17:32:54+00:00Added an answer on May 17, 2026 at 5:32 pm

    I’ve never used ExpectedException, so I don’t have any experience to share on this. An option is to Assert that it Throws directly inside the test. Something like this:

    [Test]
    public void FirstOnEmptyEnumerable()
    {
        Assert.Throws<TestCustomException>(() => this.emptyEnumerable.First(new TestCustomException()));
    }
    

    I find this approach more readable as you test for the exception exactly where you expect it to happen instead of saying “somewhere inside this function I except an exception to be thrown”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using NUnit 2.5, and I want to test a method that recieves
I'm new to TDD and xUnit so I want to test my method that
I have a method CreateAccount(...) that I want to unit test. Basically it creates
I want to test a JSF Backing-Bean method isInProgress that delegates to a service
Ok, I have an Nunit test class setup that runs though a method, lets
I want to create a method in superclass of test-class that stubs some commonly
I want to write a test that calls a remote server and validates the
I want to test my part of code that returns the users password question.
I have a rootfs boot image that I want to test by mounting on
I have a test file that contains 1,2,3 2,3,4 5,6,7 I want to insert

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.