title: Cannot throw custom exception using orElseThrow in Java
body: <p>I am trying to throw a custom exception using ternary operation and <code>orElseThrow</code> as shown below:</p>
  <pre><code>public static MainProviderType getMainProviderType(ProviderType providerType) {
      return Optional.ofNullable(MainProviderType.valueOf(providerType.name()))
          .orElseThrow(() -&gt; new ProviderTypeNotFoundException(providerType.name()));
  }
  </code></pre>
  <p>However, when <code>providerType</code> is null, this method returns <code>NullPointerException</code> rather than <code>ProviderTypeNotFoundException</code>. I think the problem is related to that; it cannot evaluate <code>MainProviderType.valueOf(providerType.name())</code> because <code>providerType.name()</code>. Is that true? And how should I use it and fix the problem?</p>