This post originated from an RSS feed registered with .NET Buzz
by Darrell Norton.
Original Post: Argument Exception utility
Feed Title: Darrell Norton's Blog
Feed URL: /error.htm?aspxerrorpath=/blogs/darrell.norton/Rss.aspx
Feed Description: Agile Software Development: Scrum, XP, et al with .NET
Nils Jonsson has posted a code sample on The Code Project that makes it easy to raise argument exceptions. You know, argument exceptions are those exceptions you are supposed to raise when the parameters passed in to your method don’t match what you thought they would be? Nils makes it easy by including the following 9 base methods (plus overloads):
ThrowIfArgumentDifferentType() throws ArgumentException if an argument is not an instance of a given type.
ThrowIfArgumentIncompatibleType() throws ArgumentException if an argument cannot be cast to a given type.
ThrowIfArgumentInvalidEnumValue() throws InvalidEnumArgumentException if an argument is not a constant in a given enumerated type.
ThrowIfArgumentNull() throws ArgumentNullException if an argument is a null reference (Nothing in Visual Basic).
ThrowIfArgumentOutOfRange() throws ArgumentOutOfRangeException if an argument is less than a given minimum or greater than a given maximum.
ThrowIfArgumentOutOfRangeExclusive() throws ArgumentOutOfRangeException if an argument is less than or equal to a given lower bound or greater than or equal to a given upper bound.
ThrowIfArgumentOutOfRangeIncludeMax() throws ArgumentOutOfRangeException if an argument is less than or equal to a given lower bound or greater than a given maximum.
ThrowIfArgumentOutOfRangeIncludeMin() throws ArgumentOutOfRangeException if an argument is less than a given minimum or greater than or equal to a given upper bound.
ThrowIfArrayArgumentDifferentRank() throws RankException if an array argument does not have a given number of dimensions.
With these methods, it takes a single line of code to check a parameter and throw the appropriate exception. I’ve been writing methods similar to this in an ad-hoc manner for a while, but now I can take advantage of someone else’s hard work. In true TDD spirit, Nils also includes 620 NUnit test cases developed with the fabulous TestDriven.NET add-in by Jamie Cansdale!
This Blog Hosted On: http://www.DotNetJunkies.com/