2015-05-05 166 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 
}