我有一個方法來加載客戶文件,從文件打開對話框中選擇它,它的工作原理,除了當我點擊取消按鈕。即使按下「取消」按鈕,它仍會加載所選文件。我想加載一個自定義異常,如果我點擊取消按鈕。任何關於如何在我的方法中實現自定義異常的幫助?由於拋出和捕獲自定義異常
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");
}
}
請不要對Java使用JavaScript堆棧片段。 –