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 7646751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:17:17+00:00 2026-05-31T10:17:17+00:00

Consider a factory method that I may or may not control, passed to another

  • 0

Consider a factory method that I may or may not control, passed to another class as a Func<T>:

// this.FactoryMethod is an external dependency passed into this class
// through constructor or property injection assume that it is not null
// but there is no guarantee that it will return a non-null reference.
Func<IModel> FactoryMethod;

public IModel GetPopulatedModel(int state, FileInfo someFile)
{       
   // Argument validation omitted for brevity...

   // This operation could return null.
   var model = this.FactoryMethod()
   if (model == null)
   {
      throw new SomeException("Factory method failed to produce a value.");
   }

   // Safe to assign to properties at this point.
   model.Priority = state;
   model.File = someFile;
   return model;
}

I would like to throw something that indicates the operation failed.

ArgumentNullException would be misleading because there is nothing wrong with the arguments:

The exception that is thrown when a null reference (Nothing in Visual
Basic) is passed to a method that does not accept it as a valid
argument.

InvalidOperationException doesn’t really seem to be on point:

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

It seems weird to consider a local variable to be part of the object's current state.

What would be good exception to throw? I’m surprised there is no OperationFailedException in System. Should I write my own exception, or is there a good one out there?

  • 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-31T10:17:19+00:00Added an answer on May 31, 2026 at 10:17 am

    Two examples from the .NET Framework:

    • The SecurityTokenParameters.Clone Method throws an InvalidOperationException if the SecurityTokenParameters.CloneCore Method returns null.

      InvalidOperationException Class

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

    • The ClientCredentials.Clone Method throws a NotImplementedException if the ClientCredentials.CloneCore Method returns null.

      NotImplementedException Class

      The exception that is thrown when a requested method or operation is not implemented.

    CloneCore is essentially a factory method for clones of the current object.

    I would use a NotImplementedException, because the factory method does not implement what is requested from it.

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

Sidebar

Related Questions

Consider this example: public class Factory { private List<ISubFactory> subFactories; public Factory(List<ISubFactory> subFactories) {
Consider a class with a static factory method, which gets a CSV (or TSV)
Consider I have a some Factory class: class Factory { public: Factory(); virtual ~Factory();
Consider the following method (used in a factory method): private Packet(byte[] rawBytes, int startIndex)
I have an interesting problem. Consider this class hierachy: class Base { public: virtual
Okay - so I know it's simple to build a factory method that provides
Consider this a huge pool of tasks: var tasks = new Task[4] { Task.Factory.StartNew(()
This is a rather simple question that is making me a bit curious. Consider
Consider this example The Interface interface IBusinessRules { string Perform(); } The Inheritors class
Consider this example: public interface IAccount { string GetAccountName(string id); } public class BasicAccount

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.