title: Why is it not allowed to throw an exception in a Java instance initialization block?
body: <p>When I try to throw an exception in an instance initialization (not class initialization) block I get the error:</p>
  
  <pre><code>initializer must be able to complete normally
  </code></pre>
  
  <p>Why is it not allowed although Java does it itself?</p>
  
  <p>The following example creates four classes. The class <code>A</code> fails during instantiation because of an ArithmeticException.  This can be and handled with a <code>catch</code>. The same for <code>B</code> which fails with a NullPointerException.  But when I try to throw a NullPointerException on my own as in <code>C</code> the program does not compile.  And I get the same error when I try to define my own RuntimeException as in <code>D</code>. So:</p>
  
  <p>How can I do the same as Java does itself?</p>
  
  <pre><code>// -*- compile-command: "javac expr.java &amp;&amp; java expr"; -*-
  
  class expr
  {
      class A
      {
          int y;
          {{ y = 0 / 0; }}
      }
  
      class B
      {
          Integer x = null;
          int y;
          {{ y = x.intValue(); }}
      }
  
      class C
      {
          {{ throw new NullPointerException(); }}
      }
  
      class Rex extends RuntimeException {}
  
      class D
      {
          {{ throw new Rex(); }}
      }
  
      void run ()
      {
          try { A a = new A(); }
          catch (Exception e) { System.out.println (e); }
  
          try { B b = new B(); }
          catch (Exception e) { System.out.println (e); }
  
          try { C c = new C(); }
          catch (Exception e) { System.out.println (e); }
  
          try { D d = new D(); }
          catch (Exception e) { System.out.println (e); }
      }
  
      public static void main (String argv[])
      {
          expr e = new expr();
          e.run();
      }
  }
  </code></pre>