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.

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. 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. 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.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be