【问题标题】:Throw and Catch Custom Exception抛出并捕获自定义异常
【发布时间】:2015-07-14 19:45:24
【问题描述】:

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

 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 堆栈 sn-ps。

标签: java file exception exception-handling fileopendialog


【解决方案1】:

您没有使用选择器选项,一旦它选择了值,只需添加一个 if 条件来检查选择器选项值,如果选择,则执行否则抛出您的异常

【讨论】:

    【解决方案2】:

    看起来您是在做作业,而不是对选择器的结果进行测试。

    代替

    chooserOption = JFileChooser.APPROVE_OPTION;
    

    你应该有

    if (chooserOption == JFileChooser.APPROVE_OPTION) {
        // handle open file
    } else {
        throw new CancelException();
    }
    

    编辑

    作为对评论的回应,异常应该扩展 Exception(用于检查的异常)、RuntimeException(用于未检查的异常)或这些类之一的后代。此级别的唯一区别是您不需要在方法签名的throws 中声明未经检查的异常。您的异常将如下所示

    public class CancelException extends Exception {
    
        public CancelException() {
        }
    
        public CancelException(String message) {
            super(message);
        }
    }
    

    另一条评论 - 例外情况应用于例外情况。使用它们来实现逻辑通常被认为是不好的做法——你真的需要使用异常吗?

    【讨论】:

    • 嗨,感谢您的回答,现在我理解了这个概念,但是当我尝试插入自定义异常时,它说“类 CancelExcpetion 中的构造函数 CancelExcpetion 不能应用于给定类型;需要字符串”
    【解决方案3】:

    让你的方法声明抛出你的异常:

    private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt)
        throws CustomException {
    

    你总是给选择器选项APPROVE_OPTION

     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);
        }
    }
    

    【讨论】:

    • 嗨,感谢您的回答,现在我理解了这个概念,但是当我尝试插入自定义异常时,它说“类 CancelExcpetion 中的构造函数 CancelExcpetion 不能应用于给定类型;需要字符串”
    • 完全正确,请查看我的更新。你必须抛出一个与给定构造函数匹配的eception:throw new CustomException("ERROR MESSAGE"); !!!!
    • 惊人的乔迪!非常感谢:)
    【解决方案4】:

    您不应将任何内容分配给chooserOption。您应该使用返回值JFileChooser.showOpenDialog(),它包含有关对话框显示结果的信息。 示例:

    int chooserOption = chooser.showOpenDialog(null);
    if (chooserOption == JFileChooser.CANCEL_OPTION) {
       // throw your exception (or do some other actions) here
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-08
      • 1970-01-01
      相关资源
      最近更新 更多