Java 抛出并捕获自定义异常
我有一种方法可以通过从“文件打开”对话框中选择客户文件来加载客户文件,它可以工作,但单击“取消”按钮时除外。即使我按下“取消”按钮,它仍会加载选定的文件。如果单击“取消”按钮,我想加载自定义异常。关于如何在我的方法中实现自定义异常,有什么帮助吗?谢谢Java 抛出并捕获自定义异常,java,file,exception,exception-handling,fileopendialog,Java,File,Exception,Exception Handling,Fileopendialog,我有一种方法可以通过从“文件打开”对话框中选择客户文件来加载客户文件,它可以工作,但单击“取消”按钮时除外。即使我按下“取消”按钮,它仍会加载选定的文件。如果单击“取消”按钮,我想加载自定义异常。关于如何在我的方法中实现自定义异常,有什么帮助吗?谢谢 private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) { Customer customerfile = null; try {
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");
}
}
您没有使用ChooseOption,一旦它选择了值,只需添加一个if条件来检查ChooseOption值,如果选择,则执行,否则抛出异常。看起来您是在对选择器的结果执行赋值而不是测试 而不是
chooserOption = JFileChooser.APPROVE_OPTION;
你应该
if (chooserOption == JFileChooser.APPROVE_OPTION) {
// handle open file
} else {
throw new CancelException();
}
编辑
作为对注释的响应,异常应该扩展Exception(对于选中的异常)、RuntimeException(对于未选中的异常)或这些类之一的后代。这个级别的唯一区别是,您不需要在方法签名的抛出中声明未检查的异常。您的异常将如下所示
public class CancelException extends Exception {
public CancelException() {
}
public CancelException(String message) {
super(message);
}
}
另一条评论——例外情况应用于例外情况。使用它们来实现逻辑通常被认为是不好的做法-您真的需要使用异常吗?做出方法声明来处理异常:
private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt)
throws CustomException {
您正在给alwaysAPPROVE_选项
选择选项:
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);
}
}
您不应将任何内容分配给选择选项
。您应该使用返回值JFileChooser.showOpenDialog()
,它包含有关对话框显示结果的信息。
例如:
请不要使用Java的Javascript堆栈片段。您好,谢谢您的回答,现在我理解了这个概念,但是当我尝试插入我的自定义异常时,它说“类中的构造函数CancelException不能应用于给定的类型;必需的字符串”您好,谢谢您的回答,现在我理解了这个概念,但是当我尝试插入我的自定义异常时,它说“constructor CancelException in class CancelException不能应用于给定的类型;required String”完全正确,请检查我的更新。必须抛出与给定构造函数匹配的eception:抛出新的CustomException(“错误消息”)代码>!!!!太棒了,乔迪!非常感谢:)
int chooserOption = chooser.showOpenDialog(null);
if (chooserOption == JFileChooser.CANCEL_OPTION) {
// throw your exception (or do some other actions) here
}