title: What exception should I throw when a factory method returns null?
body: <p>Consider a factory method that I may or may not control, passed to another class as a <code>Func&lt;T&gt;</code>:</p>
  
  <pre><code>// 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&lt;IModel&gt; 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;
  }
  </code></pre>
  
  <p>I would like to throw something that indicates the operation failed.  </p>
  
  <p><code>ArgumentNullException</code> would be misleading because there is nothing wrong with the arguments:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p><code>InvalidOperationException</code> doesn't really seem to be on point:</p>
  
  <blockquote>
    <p>The exception that is thrown when a method call is invalid for the
    object's current state.</p>
  </blockquote>
  
  <p>It seems weird to consider a local variable to be part of <code>the object's current state</code>.</p>
  
  <p>What would be good exception to throw?  I'm surprised there is no <code>OperationFailedException</code> in <code>System</code>.  Should I write my own exception, or is there a good one out there?</p>