How come you cannot catch Code Contract exceptions?
- by Finglas
System.Diagnostics.Contracts.ContractException is not accessiable 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.