How come you cannot catch Code Contract exceptions?
Posted
by Finglas
on Stack Overflow
See other posts from Stack Overflow
or by Finglas
Published on 2010-04-14T18:26:17Z
Indexed on
2010/04/14
18:33 UTC
Read the original article
Hit count: 383
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.
© Stack Overflow or respective owner