title: How to handle exception handling in singleton pattern?
body: <p>I am trying to understand how to handle exception handling here. I have my below Singleton class which connects to cassandra, initialize all the metadata on the first call and then starts a periodic background thread which updates all our metadata every 15 minutes.</p>
  
  <pre><code>public class CassUtils {
    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private final CassSession cassSession;
  
    private static class Holder {
      private static final CassUtils INSTANCE = new CassUtils();
    }
  
    public static CassUtils getInstance() {
      return Holder.INSTANCE;
    }
  
    private CassUtils() {
      CassConfig config = Utils.getConfig();
      try {
        this.cassSession = new CassSession(config);
        initializeMetadata(); // initializes metadata on the very first call
      } catch (Exception ex) {
        // log error
        throw new IllegalStateException("cannot initialize metadata: " + ex.getMessage());
      }
    }
  
    private void startScheduleTask() {
      scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
          try {
            List&lt;ProcessMetadata&gt; processMetadata = getProcessMeta();
            List&lt;ClientMetadata&gt; clientMetadata = getClientMeta();
            List&lt;ProcMetadata&gt; procMetadata = getProcMeta();
            if (!processMetadata.isEmpty() &amp;&amp; !clientMetadata.isEmpty())
              MetadataManager.setMetadata(processMetadata, clientMetadata, procMetadata);
          } catch (Exception ex) {
              // log error
          }
        }
      }, 30, 15, TimeUnit.MINUTES);
    }
  
    private void initializeMetadata() {
      List&lt;ProcessMetadata&gt; processMetadata = getProcessMeta(true);
      List&lt;ClientMetadata&gt; clientMetadata = getClientMeta();
      List&lt;ProcMetadata&gt; procMetadata = getProcMeta();
      if (processMetadata.isEmpty() || clientMetadata.isEmpty() || procMetadata.isEmpty()) {
        throw new IllegalStateException(); // is it ok to throw exception without any message here?
      }
      MetadataManager.setMetadata(processMetadata, clientMetadata, procMetadata);
      startScheduleTask();
    }
  </code></pre>
  
  <p>I need to notify the caller who is calling this singleton if there are any issues.</p>
  
  <ul>
  <li>Now for whatever reason if <code>CassSession</code> throws exception because that is the class connects to cassandra and for some reason it is not able to connect to cassandra (mostly it will never happen), then I catch the exception in <code>catch</code> block in <code>CassUtils</code> class and throw <code>IllegalStateException</code> to the users with a message in it.</li>
  <li>Also let's say if we are able to connect to cassandra through <code>CassSession</code> (which we will always) but in the <code>initializeMetadata</code> method, all our metadata are empty, then I don't want to proceed further so I am throwing <code>IllegalStateException</code> without any message in it. Is that ok? Because ultimately it will be caught by <code>catch</code> block of <code>CassUtils</code> class which is throwing exception to the calling users.</li>
  </ul>
  
  <p>How to handle exception handling in singleton cases so that we can notify calling person whoever is calling this singleton. And <code>IllegalStateException</code> is the right exception to be thrown here? These above cases will happen during the first call at the initialization time. Is there any better way to do?</p>