2011-08-09 21 views
12
public class Strange1 { 
    public static void main(String[] args) { 
    try { 
     Missing m = new Missing(); 
    } catch (java.lang.NoClassDefFoundError ex) { 
     System.out.println("Got it!"); 
    } 
    } 
} 

public class Strange2 { 
    public static void main(String[] args) { 
    Missing m; 
    try { 
     m = new Missing(); 
    } catch (java.lang.NoClassDefFoundError ex) { 
     System.out.println("Got it!"); 
    } 
    } 
} 

class Missing { 
    Missing() { } 
} 

如果您刪除Missing.class後運行Strange1和Strange2,Strange1將拋出NoClassDefFoundError;但Strange2將打印得到它!整蠱的try-catch Java代碼

任何人都可以解釋一下嗎?謝謝。

更新:

Java字節碼爲Strange1

 0 new info.liuxuan.test.Missing [16] 
    3 dup 
    4 invokespecial info.liuxuan.test.Missing() [18] 
    7 astore_1 [m] 
    8 goto 20 
    11 astore_1 [ex] 
    12 getstatic java.lang.System.out : java.io.PrintStream [19] 
    15 ldc <String "Got it!"> [25] 
    17 invokevirtual java.io.PrintStream.println(java.lang.String) : void [27] 
    20 return 
     Exception Table: 
     [pc: 0, pc: 8] -> 11 when : java.lang.NoClassDefFoundError 
     Line numbers: 
     [pc: 0, line: 14] 
     [pc: 11, line: 15] 
     [pc: 12, line: 16] 
     [pc: 20, line: 18] 
     Local variable table: 
     [pc: 0, pc: 21] local: args index: 0 type: java.lang.String[] 
     [pc: 8, pc: 11] local: m index: 1 type: info.liuxuan.test.Missing 
     [pc: 12, pc: 20] local: ex index: 1 type: java.lang.NoClassDefFoundError 

Java字節碼的Strange2

 0 new info.liuxuan.test.Missing [16] 
    3 dup 
    4 invokespecial info.liuxuan.test.Missing() [18] 
    7 astore_1 [m] 
    8 goto 20 
    11 astore_2 [ex] 
    12 getstatic java.lang.System.out : java.io.PrintStream [19] 
    15 ldc <String "Got it!"> [25] 
    17 invokevirtual java.io.PrintStream.println(java.lang.String) : void [27] 
    20 return 
     Exception Table: 
     [pc: 0, pc: 8] -> 11 when : java.lang.NoClassDefFoundError 
     Line numbers: 
     [pc: 0, line: 15] 
     [pc: 11, line: 16] 
     [pc: 12, line: 17] 
     [pc: 20, line: 19] 
     Local variable table: 
     [pc: 0, pc: 21] local: args index: 0 type: java.lang.String[] 
     [pc: 8, pc: 11] local: m index: 1 type: info.liuxuan.test.Missing 
     [pc: 12, pc: 20] local: ex index: 2 type: java.lang.NoClassDefFoundError 

只有一個地方是不同的:

11 astore_1 [ex] 

11 astore_2 [ex] 

再次更新:

每個人都可以嘗試在日食。

+0

看看生成的字節碼來查看差異。 – Thilo

+1

他們都打印「知道了!」爲了我。 –

+0

「刪除」是什麼意思?如果它在編譯時不存在,它甚至不應該編譯。請編輯您的問題以更具體。此外,標題並不意味着與這個​​問題有關的任何東西 - 你可以編輯它也更具體嗎? –

回答

2

在說什麼之前,我不會編譯這個代碼。因爲當編譯器無法找到一個類(因爲它被刪除)。可能是在使用javac命令編譯它時出現錯誤。如果那是這種情況它非常正常,絕不奇怪。

讓我再補充一點..檢查你的進口,包含缺失類。如果它在那裏,然後刪除它。並告訴我們發生了什麼。

+2

我會期待你說的是真實的,但是看起來他體驗到了相反的行爲(註釋中的所有內容都是不承認的) - 變量聲明被捕獲。 – dfb

+0

@spinning_plate ..對不起哥們。其實我錯了。 – ngesh

0

每當第一次引用(聲明或創建實例)到缺失的類時,都會引發NoClassDefFoundError。現在,拋出一個錯誤或捕獲它取決於你是否使用try-catch塊作爲你的第一個參考。

1

我創建了兩個java文件。 Strange1.java包含類Strange1和Missing。 Strange2.java包含了Strange2類。我刪除了Missing.class。 我得到了「明白!」來自兩者。

請看下面詳細信息:

[email protected]:~$ java -version 
java version "1.6.0_25" 
Java(TM) SE Runtime Environment (build 1.6.0_25-b06) 
Java HotSpot(TM) Server VM (build 20.0-b11, mixed mode) 
[email protected]:~$ gedit Strange1.java 
[email protected]:~$ gedit Strange2.java 
[email protected]:~$ javac Strange1.java 
[email protected]:~$ javac Strange2.java 
[email protected]:~$ java Strange1 
[email protected]:~$ java Strange2 
[email protected]:~$ rm Missing.class 
[email protected]:~$ java Strange1 
Got it! 
[email protected]:~$ java Strange2 
Got it! 

我在Ubuntu 11.04 Linux機器上運行它。

所以它可能是你正在使用的Java版本。

+0

我在eclipse中試過了,所以你可能會嘗試在eclipse中。但是我得到的命令是NoClassDefFoundError。我已經嘗試了JDK 1.5.0_06和1.6.0_23。 – Foredoomed

+0

我也在Eclipse Helios上試了一下,我得到了「明白!」如前所述。 –