title: Throw an exception if a Stream has no result
body: <p>I need to throw an exception inside lambda and I'm not sure how to do it.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>listOfProducts
  .stream()
  .filter(product -&gt; product.getProductId().equalsIgnoreCase(productId))
  .filter(product -&gt; product == null) //like if(product==null) throw exception
  .findFirst()
  .get()
  </code></pre>
  
  <p>I have no idea how to do that. Is there any way to do this or I just bypass it by applying filter so that filter will not forward null value like 
  <code>filter(product-&gt;product!=null)</code> (even a hint will be useful :))</p>
  
  <p><strong>Edit</strong> The actuall question is I need a product and if it is null then it will throw exception otherwise it will pass, it's not mentioned in <a href="https://stackoverflow.com/questions/18198176/java-8-lambda-function-that-throws-exception">Java 8 Lambda function that throws exception?</a></p>
  
  <p>The code I am trying to refactor is </p>
  
  <pre><code>for(Product product : listOfProducts) {
    if(product!=null &amp;&amp; product.getProductId()!=null &amp;&amp;
        product.getProductId().equals(productId)){
      productById = product;
      break;
    }
  }
  if(productById == null){
    throw new IllegalArgumentException("No products found with the
      product id: "+ productId);
  }
  </code></pre>
  
  <p>I have another possible solution as </p>
  
  <pre><code>public Product getProductById(String productId) {
          Product productById = listOfProducts.stream()
                  .filter(product -&gt; product.getProductId().equalsIgnoreCase(productId)).findFirst().get();
  
          if (productById == null)
              throw new IllegalArgumentException("product with id " + productId + " not found!");
  
          return productById;
      }
  </code></pre>
  
  <p>But I want to solve it using functional interface and it will be good if I can achieve this using one line in this method like</p>
  
  <pre><code>...getProductById()
  return stream...get();
  </code></pre>
  
  <p>and If I need to declare a custom method to declare exception, it won't be an issue</p>