title: How come you cannot catch Code Contract exceptions?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>TestMethod</strong></p>
  
  <pre><code>[TestMethod, ExpectedException(typeof(System.Diagnostics.Contracts.ContractException))]
  public void returning_a_value_less_than_one_throws_exception()
  {
      var person = new Person();
      person.Number();
  }
  </code></pre>
  
  <p><strong>Method</strong></p>
  
  <pre><code>public int Number()
  {
      Contract.Ensures(Contract.Result&lt;int&gt;() &gt;= 0);
      return -1;
  }
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre>Error 1 'System.Diagnostics.Contracts.ContractException' is inaccessible
  due to its protection level.</pre>
  
  <p><strong>Edit</strong></p>
  
  <p>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.</p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(ArgumentException))]
  public void value_input_must_be_greater_than_zero()
  {
      // Arrange
      var person = new Person();
      // Act
      person.Number(-1);
  }
  </code></pre>
  
  <p>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.</p>