title: Class Constructor fails throwing Exception on Class Loading
body: <p>I have a constructor that fails on the first call to <code>new</code> because an Exception of class <code>javax.persistence.NoResultException</code> being catch in one of the Class' methods. </p>
  
  <p><strong>EDIT: It was not a NoResultException, the NoResultException was the message of the Exception. The stackTrace is below</strong></p>
  
  <p>If I remove the import to <code>javax.persistence.NoResultException</code> and catch just a generic <code>Exception e</code>, the code works.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Why is the code inside a method which hasn't been called is affecting the Constructor? (is not being called in the constructor either)</li>
  <li>Why is the ClassLoader throwing an exception which is not supposed to throw? <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#loadClass%28java.lang.String%29" rel="nofollow noreferrer">ClassLoader.loadClass()</a> throws <code>ClassNotFoundException</code> according to JavaDoc.</li>
  </ol>
  
  <p>that catch for javax.persistence.NoResultException as well as the import are old code which managed to survive between versions, I removed it and it's fixed, however I'd like to now the reason behind this.</p>
  
  <p><strong>STACKTRACE:</strong> </p>
  
  <pre><code>java.lang.NoClassDefFoundError: javax/persistence/NoResultException
      at xxx.xxxxxx.xxxxxxx.xxxx.xxxx.xxxxxxxxxxxx.xxxxxxxxxxxxxxx(ClassB.java)
      ...
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
      at java.util.concurrent.FutureTask.run(FutureTask.java:138)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: java.lang.ClassNotFoundException: javax.persistence.NoResultException
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
      ... 10 more
  </code></pre>
  
  <p><strong>Go to the end to see the step by step images of the debugger and the moment it fails</strong></p>
  
  <p><strong>FAILING CODE:</strong></p>
  
  <pre><code>// ClassB.java
  
  import javax.persistence.NoResultException; // NOTICE THE IMPORT
  
  public ClassB{
      private UserInfo user;
      private WebServiceBDelegate port;
      private Connection conn;
  
      public ClassB (UserInfo user, Connection conn) throws Exception {
          System.out.println("CLASS B CONSTRUCTOR");
          this.user = user;
          this.conn = conn;
          this.port = new WebServiceBService().getForwardingPort();
      }
  
      public boolean methodB(List&lt;String&gt; list){
          try{
              // Check some stuff on database using this.conn
          } catch(NoResultException nre){         // NOTICE THE CATCH
              String something = getSomething();
          }
          // Get the values to invoke SOAP service using this.conn
          status = port.operationB(values);
          if(status &gt; 0)
              return true;
          return false;
      }
  }
  </code></pre>
  
  <p><strong>WORKING CODE:</strong></p>
  
  <pre><code>// ClassB.java
  
  public ClassB{
      private UserInfo user;
      private WebServiceBDelegate port;
      private Connection conn;
  
      public ClassB (UserInfo user, Connection conn) throws Exception {
          System.out.println("CLASS B CONSTRUCTOR");
          this.user = user;
          this.conn = conn;
          this.port = new WebServiceBService().getForwardingPort();
      }
  
      public boolean methodB(List&lt;String&gt; list){
          try{
              // Check some stuff on database using this.conn
          } catch(Exception e){         // THIS IS THE CHANGED CATCH
              String something = getSomething();
          }
          // Get the values to invoke SOAP service using this.conn
          status = port.operationB(values);
          if(status &gt; 0)
              return true;
          return false;
      }
  }
  </code></pre>
  
  <p>Here are the step by step images from the debugger when entering INTO the class constructor, to the JVM default ClassLoader and the moment it fails:</p>
  
  <p><a href="https://i.stack.imgur.com/a1z4N.png" rel="nofollow noreferrer">STEP INTO when calling new ClassB gets me to loadClassInternal(String)</a>
  <img src="https://i.stack.imgur.com/a1z4N.png" alt="STEP INTO when calling new ClassB gets me to loadClassInternal(String)"></p>
  
  <p><a href="https://i.stack.imgur.com/e2CEu.png" rel="nofollow noreferrer">STEP INTO loadClassInternal(String), gets me to loadClass(String,boolean)</a>
  <img src="https://i.stack.imgur.com/e2CEu.png" alt="STEP INTO loadClassInternal(String), gets me to loadClass(String,boolean)"></p>
  
  <p><a href="https://i.stack.imgur.com/E0IUQ.png" rel="nofollow noreferrer">STEP INTO loadClass(String,boolean), as far as I can get, now STEP RETURN</a>
  <img src="https://i.stack.imgur.com/E0IUQ.png" alt="STEP INTO loadClass(String,boolean), as far as I can get, now STEP RETURN"></p>
  
  <p><a href="https://i.stack.imgur.com/d39PK.png" rel="nofollow noreferrer">STEP RETURN gets me to loadClass(String</a>)
  <img src="https://i.stack.imgur.com/d39PK.png" alt="STEP RETURN gets me to loadClass(String)"></p>
  
  <p><a href="https://i.stack.imgur.com/NjiwW.png" rel="nofollow noreferrer">STEP OVER gets me to loadClassInternal(String)</a>
  <img src="https://i.stack.imgur.com/NjiwW.png" alt="STEP OVER gets me to loadClassInternal(String)"></p>
  
  <p><a href="https://i.stack.imgur.com/C9N4C.png" rel="nofollow noreferrer">STEP OVER gives me javax.persistence.NoResultException</a>
  <img src="https://i.stack.imgur.com/C9N4C.png" alt="STEP OVER gives me javax.persistence.NoResultException"></p>
  
  <p>STEP OVER TAKES ME BACK TO A FINALLY IN MY CODE AND ENDS EXECUTION</p>