Assume I have a class like this:
public class Foo
{
public Bar RequiredProperty { get; set;}
public void Baz()
{
if (this.RequiredProperty == null)
{
// Which exception should I throw?
}
}
}
My solution has a class that is designed to be reused without having to pass a lot of arguments to the Bar
method over and over again. So, what should I throw when Bar
isn't initialized to a non-null value?
More Information
I'm essentially rolling my own code parser and formatter. Call it an object lesson. One of the classes is an HtmlCodeFormatter
that has the following properties (in honor of Dependency Injection):
public IFormatter Formatter { get; set; }
public IParser Parsre { get; set; }
This allows me to write any number of language-specific parsers and formatters. For example, I have a CSharpParser
and a JavascriptParser
. I also have an HtmlCodeFormatter
, and there are plans for another (of dubious utility).
The idea is that you can instantiate the HtmlFormatter
using an object initializer, like this:
var formatter = new HtmlCodeFormatter()
{
Parser = new CSharpParser();
Formatter = new HtmlCodeFormatter();
};
formatter.Format("Console.WriteLine(\"Hello, world!\"));
When HtmlCodeFormatter.Format
is invoked, it needs to be able to verify that both a parser and a formatter have been provided. That's no problem, really, but I'm kind of stumped about which exception to throw. I'm leaning towards InvalidOperationException
, but I'm not entirely certain that's the best choice.