【问题标题】:Custom Exception for a load method加载方法的自定义异常
【发布时间】:2015-11-09 20:22:59
【问题描述】:

我创建了一个自定义异常,以便当我浏览文件并关闭浏览器时,我会捕获错误并告诉用户“请选择一个文件”:

public class CancelException extends Exception {

    public CancelException() {
    }

    public CancelException(String message) {
        super(message);
    }

}

现在我试图在加载方法中输入它,但我看不到我可以在哪里输入它,因为已经有多个由 netbeans 自动生成的异常:

public static Person loadcons() throws IOException
{
    Person loadcons = null;
    JFileChooser chooser = new JFileChooser();
    int chooserOption = chooser.showSaveDialog(null);
    chooserOption = JFileChooser.APPROVE_OPTION;

    try {
        File file = new File (chooser.getSelectedFile().getAbsolutePath());
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
        loadcons = (Person) input.readObject();
        input.close();
        return loadcons;
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
    }
    return null;

}

private String toString(String PersonID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

如果我是这门语言的初学者,我将不胜感激。

我尝试在加载方法中创建异常:

  public static Person loadcons() throws IOException, CancelException {
    Person loadcons = null;
    try {
        JFileChooser chooser = new JFileChooser();
        int chooserOption = chooser.showSaveDialog(null);
       if (chooserOption == JFileChooser.APPROVE_OPTION)
       {

        File file = new File(chooser.getSelectedFile().getAbsolutePath());
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));

        loadcons = (Person) input.readObject();
        input.close();
        return loadcons;
       }
       else{
           throw new CancelException("ERROR MESSAGE");
       }
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
    }
    return null;

}

private String toString(String PersonID) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

但是没有抛出异常

【问题讨论】:

    标签: java exception serialization exception-handling custom-exceptions


    【解决方案1】:

    我想在下面建议使用您的自定义异常--

    import *.CancelException;
    
    public static Person loadcons() throws IOException,CancelException 
      ..
      ..
    try {
        ..
        ..
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (ClassNotFoundException ex) {
        System.out.println(ex.getMessage());
        }catch (Exception ex) {
            throw new CancelException("Please select a file");
       }
        return null;
    

    【讨论】:

    • 我尝试过这种方式,但仍然没有发现错误,我不明白为什么,代码没有发出错误
    • 当我创建导入 ryan_assignment_sit2.CancelException;它告诉我他们有相同的包,所以我可以删除它
    • 只删除 IOException 的 catch 块然后重新检查.. 另外,如果您的 Exception 类在同一个包中,则无需导入
    • 同样的问题,好像没有异常,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2019-02-25
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多