Code Contracts: Contract.Requires<TException> (3 of n)

Posted on Monday, July 12, 2010 9:38 PM

The previous post on Code Contracts was about Contract.Requires. That method comes with an overload, which is a generic one that takes an Exception type as the generic type parameter. The purpose of this method is mainly in line with that of the Contract.Requires method that I explained in the previous post here.

The original code sample contained a few ArgumentExceptions that were thrown if certain conditions were not met. The use of the Contract.Requires method made the code more succinct with less clutter, but the exceptions were nowhere to be found. In case the precondition is not met in the code that uses Contract.Requires, a ContractException will be thrown at runtime. Sometimes it is feasible though to throw a more specific exception, like the ArgumentExceptions that were thrown in the IF-THEN-THROW constructions.

Just to step back to the code we had initially:

    public string RepeatFirstCharacter(string input, int count)
    {
      if (input == null)
      {
        throw new ArgumentNullException("input");
      }

      if (input.Length < 1)
      {
        throw new ArgumentException("The input should have at least one character.", "input");
      }

      if (count <= 0)
      {
        throw new ArgumentOutOfRangeException("count");
      }

      return new string(input.ToCharArray(0, 1)[0], count);
    }

This code is very specific about the exceptions. About the ArgumentOutOfRangeException one could argue that it is actually not a correct exception to throw here, since there is no specific range, but beyond that, the exceptions serve a purpose. It is up to the requirements of the final code and build whether or not the exception must be of the  ArgumentException family, or it should be a more ‘general’ ContractException, since it involves the predefined contract. When it is necessary to have the existing exceptions, one should use the Contract.Requires<TException> overload. The example code would end up looking like this:

    public string RepeatFirstCharacter(string input, int count)
    {
      Contract.Requires<ArgumentNullException>(input != null);
      Contract.Requires<ArgumentException>(input.Length >= 0, "The input should have at least one character.");
      Contract.Requires<ArgumentOutOfRangeException>(count > 0);

      return new string(input.ToCharArray(0, 1)[0], count);
    }

Beyond the fact that the more specific exception is used on contract failure, all other points mentioned in the post on Contract.Requires, also apply to Contract.Requires<TException>. The overload has an additional caveat though. The exception type that is to be thrown must have a public constructor accepting a single string argument. If such a constructor can not be determined at runtime, an internal ContractException will be created and thrown instead.

To conclude the posts on preconditions, the next will handle about Contract.EndContractBlock(). Even though it is not specific for preconditions, it will only truly serve a purpose when it appears at the end of legacy preconditions that are stated in the form of IF-THEN-THROW.

That’s it for now.
Meile

Feedback

# cheap jewellry

2/14/2012 10:00 AM by cheap jewellry
wrote Google+ chief Bradley Horowitz in a recent online post. post. <u><a href="http://www.nikejordanforcheap.com/">jordan">http://www.nikejordanforcheap.com/">jordan shoes for cheap</a></u> post. "Over the next week, we'll be adding support for
names – be they nicknames, birth names, or names in in <b><a href="http://www.nikejordanforcheap.com/">jordan">http://www.nikejordanforcheap.com/">jordan sneakers</a></b> in another script – alongside your common name."Activision Blizzard abandoned

# Gucci Outlet

3/26/2012 10:30 AM by Gucci Outlet
After reading this post. Our http://www.gucciofficialoutlet.com is a well identified brand valued and loved by some individuals around the world.

# longchamp outlet

3/28/2012 8:19 AM by longchamp wallet
For longchamp online details on the core papers and other statistical characteristics of each Front, see the Research Fronts section of the Essential Science Indicators from Thomson Reuters. Some papers have comments sent in by the author(s) of the paper which may include Longchamp luxury bags images and descriptions of their work. http://www.longchampparis1948.com/

# Monster Beats Headphones

4/6/2012 5:00 AM by cheap beats by dre
Thanks for the great work! Cool blog. There are a number of opinions on this topic and this blog states the problem extremely great. I will bookmark and spread the word around for you.

Post Comment

Title  
Name  
Url
Comment   

ATTENTION: the code you need to copy is CaSe SeNsItIvE and is required to prevent spam.
Enter the code you see:

posts - 24, comments - 125, trackbacks - 0, articles - 0

Copyright © Meile Zetstra