2015-05-05 113 views
2

我有一個方法來加載客戶文件,從文件打開對話框中選擇它,它的工作原理,除了當我點擊取消按鈕。即使按下「取消」按鈕,它仍會加載所選文件。我想加載一個自定義異常,如果我點擊取消按鈕。任何關於如何在我的方法中實現自定義異常的幫助?由於拋出和捕獲自定義異常

private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) { 
    Customer customerfile = null; 
    try { 

    final JFileChooser chooser = new JFileChooser("Customers/"); 
    int chooserOption = chooser.showOpenDialog(null); 
    chooserOption = JFileChooser.APPROVE_OPTION; 

    File file = chooser.getSelectedFile(); 
    ObjectInputStream in = new ObjectInputStream(
     new FileInputStream(file) 
    ); 

    customerfile = (Customer) in .readObject(); 

    custnameTF.setText(customerfile.getPersonName()); 
    custsurnameTF.setText(customerfile.getPersonSurname()); 
    custidTF.setText(customerfile.getPersonID()); 
    custpassidTF.setText(customerfile.getPassaportID()); 
    customertellTF.setText(customerfile.getPersonTel()); 
    customermobTF.setText(customerfile.getPersonMob()); 
    consnameTF.setText(customerfile.getConsultantname()); 
    conssurnameTF.setText(customerfile.getConsultantsurname()); 
    considTF.setText(customerfile.getConsulid()); 

    in .close(); 

    } catch (IOException ex) { 
    System.out.println("Error Loading File" + ex.getMessage()); 
    } catch (ClassNotFoundException ex) { 
    System.out.println("Error Loading Class"); 
    } finally { 
    System.out.println("Customer Loaded"); 
    } 

} 
+0

請不要對Java使用JavaScript堆棧片段。 –

回答

2

讓你的方法聲明特羅您的例外:

private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) 
    throws CustomException { 

你總是給APPROVE_OPTION到chooserOption:

chooserOption = JFileChooser.APPROVE_OPTION; 

必須進行對話按鈕偵聽修改這個變量,並添加一個條件:

if (chooserOption == JFileChooser.APPROVE_OPTION) { 
    // load file 
} else { 
    throw new CustomException("ERROR MESSAGE"); 
} 

而你的CustomException類必須看起來像:

class CustomExceptionextends Exception { 
    public CustomException(String msg) { 
     super(msg); 
    } 
} 
+0

嗨,感謝您的回答,現在我理解了這個概念,但是當我嘗試插入自定義異常時,它說「構造函數CancelExcpetion中的CancelExcpetion不能應用於給定的類型;必需的字符串」 –

+1

完全正確,請檢查我的更新。你必須拋出一個與給定的構造函數相匹配的eception:拋出新的CustomException(「ERROR MESSAGE」);'!!!! –

+1

令人驚歎的Jordi!非常感謝 :) –

0
您不使用chooserOption,一旦選定值,只需添加一個,如果條件檢查chooserOption價值,如果選擇了,否則執行拋出你的異常

3

它看起來像你做一個任務而不是測試選擇器的結果。

而不是

chooserOption = JFileChooser.APPROVE_OPTION; 

你應該有

if (chooserOption == JFileChooser.APPROVE_OPTION) { 
    // handle open file 
} else { 
    throw new CancelException(); 
} 

編輯

在迴應的意見,異常應該延長或者異常(checked異常),RuntimeException的(對於未經檢查的例外)或其中一個類的後代。這個級別唯一的區別是您不需要在方法簽名的throws中聲明未檢查的異常。你的例外看起來像這樣

public class CancelException extends Exception { 

    public CancelException() { 
    } 

    public CancelException(String message) { 
     super(message); 
    } 
} 

一個其他評論 - 異常應該用於特殊情況。使用它們來實現邏輯通常被認爲是不好的做法 - 你是否真的需要使用異常?

+0

嗨,感謝您的回答,現在我理解了這個概念,但是當我嘗試插入自定義異常時,它說「類CancelExcpetion中的構造函數CancelExcpetion不能應用於給定的類型;必需的字符串」 –

+1

請參閱編輯答案。 –

+0

謝謝史蒂夫! :) –

1

你不應該分配任何chooserOption。您應該使用返回值JFileChooser.showOpenDialog(),它包含有關對話框顯示結果的信息。 示例:

int chooserOption = chooser.showOpenDialog(null); 
if (chooserOption == JFileChooser.CANCEL_OPTION) { 
    // throw your exception (or do some other actions) here 
}