The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Argument Exception utility

0 replies.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 0 replies on 1 page
Darrell Norton

Posts: 876
Nickname: dnorton
Registered: Mar, 2004

Darrell Norton is a consultant for CapTech Ventures.
Argument Exception utility Posted: Dec 13, 2004 8:48 PM
Reply to this message Reply

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
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Darrell Norton
Latest Posts From Darrell Norton's Blog

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/

Read: Argument Exception utility


Topic: COM Interop with Whidbey Generics? Previous Topic   Next Topic Topic: Full Text Search in SQL 2005 - the teen age

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use