我有一個構造函數在第一次調用new
時失敗,因爲類javax.persistence.NoResultException
的異常被捕獲到一個類的方法中。類構造函數無法拋出類加載異常
編輯:它不是NoResultException,NoResultException是異常的消息。 stackTrace低於
如果我刪除導入到javax.persistence.NoResultException
並只捕獲一般Exception e
,代碼工作。
我的問題是:
- 爲什麼一個方法裏面的代碼還沒有被稱爲是影響構造? (也沒有在構造函數中調用)
- 爲什麼ClassLoader拋出一個不應該拋出的異常?根據JavaDoc,ClassLoader.loadClass()拋出
ClassNotFoundException
。
,對於javax.persistence.NoResultException趕上以及進口老代碼管理版本之間生存,我刪除它和它的固定的,但是我想現在這背後的原因。
堆棧跟蹤:
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的時刻:
// 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<String> 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 > 0)
return true;
return false;
}
}
WORKING 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<String> 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 > 0)
return true;
return false;
}
}
下面是調試器一步一步圖片進入類的構造函數時,在JVM默認的ClassLoader的那一刻失敗:
STEP INTO when calling new ClassB gets me to loadClassInternal(String)
STEP INTO loadClassInternal(String), gets me to loadClass(String,boolean)
STEP INTO loadClass(String,boolean), as far as I can get, now STEP RETURN
STEP RETURN gets me to loadClass(String)
STEP OVER gets me to loadClassInternal(String)
STEP OVER gives me javax.persistence.NoResultException
STEP OVER把我帶回到甲終於在我的代碼,並結束EXECUTION
可能重複http://stackoverflow.com/questions/2613113/javax-persistence-noresultexception-getsingleresult-did-not-retrieve-any-enti – amicngh 2012-07-31 10:59:38
這不是重複的,一個人的交易,不同之處是拋出,這似乎是關於類加載/類路徑問題。 – 2012-07-31 11:01:05