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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:26:21+00:00 2026-05-14T06:26:21+00:00

System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself

  • 0

System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself messing around with my shiney new copy of Visual Studio, but I’d like to know what I’m doing wrong.

I’m using the professional edition of VS, therefore I do not have static checking. In order to still use code contracts (which I like) I figured the only way my method can work is to catch the exception that is thrown at runtime, but I’m not finding this possible.

TestMethod

[TestMethod, ExpectedException(typeof(System.Diagnostics.Contracts.ContractException))]
public void returning_a_value_less_than_one_throws_exception()
{
    var person = new Person();
    person.Number();
}

Method

public int Number()
{
    Contract.Ensures(Contract.Result<int>() >= 0);
    return -1;
}

Error

Error 1 'System.Diagnostics.Contracts.ContractException' is inaccessible
due to its protection level.

Edit

After some more thought I’ve come to the conclusion discussed in the comments, as well as the following. Given a method, if this had a requirement which could be expressed in Code Contract form, I’d write tests as such.

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void value_input_must_be_greater_than_zero()
{
    // Arrange
    var person = new Person();
    // Act
    person.Number(-1);
}

This would ensure the contract is part of the code, and will not be removed. This would require the Code Contract to actually throw the specified exception however. In some cases this would not be required however.

  • 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-14T06:26:21+00:00Added an answer on May 14, 2026 at 6:26 am

    This is deliberate – although a slight pain for testing.

    The point is that in production code you should never want to catch a contract exception; it indicates a bug in your code, so you shouldn’t be expecting that any more than arbitrary unexpected exceptions which you may want to catch right at the top of your call stack so you can move onto the next request. Basically you shouldn’t view contract exceptions as ones which can be “handled” as such.

    Now, for testing that’s a pain… but do you really want to test your contracts anyway? Isn’t that a bit like testing that the compiler stops you from passing in a string to a method which has an int parameter? You’ve declared the contract, it can be documented appropriately, and enforced appropriately (based on settings, anyway).

    If you do want to test contract exceptions, you can either catch a bare Exception in the test and check its full name, or you can mess around with the Contract.ContractFailed event. I would expect unit testing frameworks to have built-in support for this over time – but it’ll take a little while to get there. In the meantime you probably want to have a utility method to expect a contract violation. One possible implementation:

    const string ContractExceptionName =
        "System.Diagnostics.Contracts.__ContractsRuntime.ContractException";
    
    public static void ExpectContractFailure(Action action)
    {
        try
        {
            action();
            Assert.Fail("Expected contract failure");
        }
        catch (Exception e)
        {
            if (e.GetType().FullName != ContractExceptionName)
            {
                throw;
            }
            // Correct exception was thrown. Fine.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this code: System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(); System.Reflection.MethodBase methodBase = stackFrame.GetMethod(); stackFrame.GetFileName(); stackFrame.GetFileLineNumber();
I'm using the Code Contracts classes in the System.Diagnostics.Contracts namespace to define some contracts
after read this article i tried generate EF model by System.Diagnostics.Process: Process myProcess =
After reading about the System.Diagnostics.Contracts.Contract static class that has been influenced by the awesomeness
When I run System.Diagnostics.Process.Start from my console application it works but the same code
I'm creating new processes using System.Diagnostics.Process class from my application. I want this processes
I've been doing some performance testing around the use of System.Diagnostics.Debug, and it seems
Consider the following code: using System; using System.Diagnostics; namespace Demo { class Program {
I have the following code using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace
We use Microsoft's code contracts in our codebase. A project had Contract.Requires statements 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.