我有一种方法可以通过从“文件打开”对话框中选择来加载客户文件,并且它可以工作,除非我单击“取消”按钮。即使我按下取消按钮,它仍会加载所选文件。如果单击取消按钮,我想加载自定义异常。请对如何在我的方法中实现自定义异常有任何帮助吗?谢谢
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");
}
}