2015-03-31 44 views
1

所以我的代碼如下所示:爲什麼我的編譯器不停地說我有一個catch塊之前沒有try塊

try { 
    t.delete("word"); 
    result = t.getRootItem().getWord().equals("humpty"); 
} catch (Exception e) { 
    result = false; 
} 

的問題是我的編譯器口口聲聲說我有一個抓之前沒有嘗試,但我確實有以前的嘗試,所以有什麼不對?這裏是我的整個主方法(我也可以張貼整個類,如果你想:

public static void main(String args[]) { 
    BSTRefBased t; 
    AbstractBinaryTree tt; 
    int i; 
    boolean result; 
    String message; 

    message = "Test 1: inserting 'word0' -- "; 
    t = new BSTRefBased(); 
    try { 
     t.insert("word0"); 
     result = t.getRootItem().getWord().equals("word0"); 
    } catch (Exception e) { 
     result = false; 
    } 
    System.out.println(message + (result ? "passed" : "FAILED")); 

    message = "Test 2: inserting 'word1', 'word2', 'word3' -- "; 
    t = new BSTRefBased(); 
    try { 
     t.insert("word1"); 
     t.insert("word2"); 
     t.insert("word3"); 
     result = t.getRootItem().getWord().equals("word1"); 
     tt = t.detachLeftSubtree(); 
     result &= tt.getRootItem().getWord().equals("word2"); 
     tt = t.detachRightSubtree(); 
     result &= tt.getRootItem().getWord().equals("word3"); 
    } catch (Exception e) { 
     result = false; 
    } 
    System.out.println(message + (result ? "passed" : "FAILED")); 

    message = "Test 3: deleting 'word3'"; 
    t = new BSTRefBased 
    try { 
     t.delete("word3"); 
     result = t.getRootItem().getWord().equals("word3"); 
    } catch (Exception e) { 
     result = false; 
    } 
    System.out.println(message + (result ? "passed" : "FAILED")); 
} 
+0

ÿ我們的代碼看起來很好 - 問題可能在其他地方(也許是在類似的字段聲明後丟失了';')。 – assylias 2015-03-31 20:57:34

回答

1

此行似乎是不正確的:

t = new BSTRefBased 

沒有()的構造函數調用,並有別無分號。它立刻是在try之前,這些錯誤必須搞亂了解析器,使得它不再承認try。嘗試

t = new BSTRefBased(); // or a similar constructor call