我有一个方法来加载客户文件,从文件打开对话框中选择它,它的工作原理,除了当我点击取消按钮。即使按下“取消”按钮,它仍会加载所选文件。我想加载一个自定义异常,如果我点击取消按钮。任何关于如何在我的方法中实现自定义异常的帮助?由于抛出和捕获自定义异常
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堆栈片段。 –