2012-03-02 61 views
0

我想從兩個文本框中獲得總和結果。我想要一種在結果文本框中返回nill的方法。爲什麼下面的代碼拋出「cath沒有嘗試」的錯誤。我錯在哪裏,請協助。沒有嘗試錯誤的抓取

private void AddButton4ActionPerformed(java.awt.event.ActionEvent evt) { 
    try 
    { 
    int x = Integer.parseInt(FirstNumberTextField.getText()); 
    int y = Integer.parseInt(SecondNumberTextField.getText()); 
    ResultTextField1.setText((x + y)+""); 
    { 
    catch(Exception e) 
    { 
     ResultTextField1.setText(""); 
    }  
} 
+2

平衡{}。 – adatapost 2012-03-02 11:34:23

+0

netbeans沒有幫助找到那個錯誤?奇怪,很奇怪...... – 2012-03-02 11:40:23

+0

可能是這樣做的,但這會是它遇到的第一個錯誤。最後一個會丟失},它們之間的任意數量。 – 2012-03-02 12:08:37

回答

1

你在try塊的最後有一個開放的大括號,而不是一個右大括號。

{ 
    catch(Exception e) 

} 
    catch(Exception e) 
+0

問題已解決感謝所有 – user1244940 2012-03-06 09:08:50

6

你一個開括號太多:

private void AddButton4ActionPerformed(java.awt.event.ActionEvent evt) { 
    try 
    { 
    int x = Integer.parseInt(FirstNumberTextField.getText()); 
    int y = Integer.parseInt(SecondNumberTextField.getText()); 
    ResultTextField1.setText((x + y)+""); 
    } // <-- This one was wrong. 
    catch(Exception e) 
    { 
     ResultTextField1.setText(""); 
    }  
} 
0

的支柱前的catch塊必須結束花},但你寫另一個大括號{。事實上所有的塊(如果,否則,嘗試,捕捉,最後,同時,對等)總是在{}

+0

Downvoter,有什麼問題? – 2012-03-05 05:59:24

1

平衡序列您在try塊的末端有一個{而不是}。這將catch塊放在裏面。