Blog

Which .NET Framework exceptions to throw?

January 22, 2007

A very useful subject today on the Visual Studio Code Analysis Team Blog called: What exception should I throw instead of the reserved exceptions that DoNotRaiseReservedExceptionTypes warns against?. It's sort of a follow-up to 'The case against catch(Exception ex)'.

I must admit that I didn't know about the InvalidEnumArgumentException class. For checking the enum arguments of my public methods, I always used the ArgumentOutOfRangeException. Yes, I learned something today!

I've seen lot's of code throwing an ArgumentNullException when an empty string is passed to a method. I guess this is what you get when you sneak in methods like String.IsNullOrEmpty into the .NET Framework. I'm very pleased that the FxCop dudes put the record straight:

When validating a parameter that is otherwise invalid (such as an empty string): throw System.ArgumentException

What always helped me a lot is Lutz Roeder's .NET Reflector. Just do what the framework does.

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be