title: How to customize the error message for NoSuchElementException in Optional class
body: <p>I have a method that throws <code>NotFoundException</code> if my object id is <code>null</code>.</p>
  
  <pre><code>public void removeStatementBundleService(String bundleId) throws NotFoundException {
      Optional&lt;StatementBundle&gt; bundleOpt = statementBundleRepository.findById(bundleId);
  
      if(bundleOpt.isPresent()) {
          StatementBundle bundle = bundleOpt.get();
  
          if(bundle.getStatements() != null &amp;&amp; !bundle.getStatements().isEmpty()) {               
              for(Statement statement: bundle.getStatements()) {
                  statementRepository.delete(statement);
              }
          }
  
          if(bundle.getFileId() != null) {
              try {
                  fileService.delete(bundle.getFileId());
              } catch (IOException e) {
                  e.printStackTrace();
              }   
          }
  
          statementBundleRepository.delete(bundle);
  
      } else {
          throw new NotFoundException("Statement bundle with id: " + bundleId + " is not found.");
      }
  }
  </code></pre>
  
  <p>I found out that this isn't needed since the <code>java.util.Optional</code> class is used. In the oracle documentation I found out that if <code>get()</code> is used and there is no value then <code>NoSuchElementException</code> is thrown. What is the best way I can add my error message to the exception. I am trying to open the <code>Optional</code> class in Eclipse to try change inside it(not sure if this is good practice) but Eclipse will not let me access this class on the other hand I read that this class is also final.</p>