Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:40:43+00:00 2026-05-20T09:40:43+00:00

Assume I have a class like this: public class Foo { public Bar RequiredProperty

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:40:43+00:00Added an answer on May 20, 2026 at 9:40 am

    I would throw a InvalidOperationException. The MSDN definition is:

    The exception that is thrown when a
    method call is invalid for the
    object’s current state.

    However, i recommend using constructor injection instead of setter injection. This makes sure you has valid parsers and formatters. If null is passed in the constructor, throw a ArgumentNullException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a class like this: public class Server { public static void
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have legacy classes, that can't be modified: class Foo { public void
Assume you have several arbitrary classes like below: class Foo { $foovar; public function
Assume I have the following code: public static class Foo { public static void
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Say you have a C++ class like: class Foo { public: virtual ~Foo() {}
Assume I have a controller method like this: [Audit] public JsonNetResult List(int start, int
Assume I have a class foo, and wish to use a std::map to store
Let's assume I have three classes that are subclasses of a base class: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.