title: How to catch exception thrown in static initializer block
body: <p>I wrote the following code:</p>
  
  <pre><code>    static {
          /* Attempts to load JDBC driver */
          try {
              Class.forName("com.mysql.jdbc.Driver");
          } catch (ClassNotFoundException e) {
              throw new DBConfigurationException("JDBC Driver not found.", e);
          }
          /* Attempts to load configuration */
          conf = loadConfiguration(); //this may throw some subclasses of RuntimeException
      }
  </code></pre>
  
  <p>because I want to JDBC driver and configuration to be loaded only once.</p>
  
  <p>I wanted to do something like this at the startup (I will simplify as much as possible):</p>
  
  <pre><code>public static void main(String[] args) {
      try {
          // load the class that contains the code above
      } catch (DBConfigurationException e) {
          // display proper error message using JOptionPane, then quit
      } catch (MissingConfigurationException e) {
          // display proper error message using JOptionPane
          // show a JDialog and allow user to input and store a configuration
      } catch (InvalidConfigurationException e) {
          // display proper error message using JOptionPane
          // show a JDialog and allow user to input and store a configuration
      }
  
      /* if everything it's ok */
      // do some other checks in order to decide which JFrame display first.
  }
  </code></pre>
  
  <p>Now, the problem is that if an exception occur, JVM will throw ad <code>ExceptionInInitializerError</code> and will just not construct the object.
  Probably I may still understand what went wrong, catching <code>ExceptionInInitializerError</code> (even if that just sounds wrong to me) and checking its cause (I still did not try to do so but I think it's possible).</p>
  
  <p>I need that object, because in case the exception is recoverable (e.g. MissingConfigurationException) the program will not exit and will need that object.</p>
  
  <p>Should I avoid the static initializer?
  I could do something like:</p>
  
  <pre><code>private static final Configuration conf = null;
  
  Constructor() {
      if (conf == null) {
          /* Attempts to load JDBC driver */
          try {
              Class.forName("com.mysql.jdbc.Driver");
          } catch (ClassNotFoundException e) {
              throw new DBConfigurationException("JDBC Driver not found.", e);
          }
          /* Attempts to load configuration */
          conf = loadConfiguration();
      }
  }
  </code></pre>
  
  <p>But even this one sounds incorrect to me: exceptions may be thrown <em>only at the first attempt of use</em> (which I know will be at startup because I have to do that checks), <strong>that is</strong> when the class is loaded. So it would be theoretically more correct the first way. :\</p>
  
  <p>What should I do? Which way is more correct?</p>
  
  <p>The problem is that the class with static initializer <em>NEEDS</em> both driver and configuration, so it <em>SHOULDN'T</em> be used before that both of them are available. :\</p>