未报告的IOException,即使在try-catch块中也会抛出异常

时间:2013-11-25 04:55:04

标签: java ioexception

我的代码有未报告的IOExceptions,即使我在try-catch块中有它,并且方法名称我抛出了IOException

public static void addStudent() throws Exception
{
    try
    {
        final JFrame frame = new JFrame("Add Details");
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(
          JFrame.EXIT_ON_CLOSE);

        final JTextField tf1 = new JTextField("Input Student's name here", 25);
        final JTextField tf2 = new JTextField("Input Student's ID number here", 25);
        final JTextField tf3 = new JTextField("Input Student's Email Address here", 25);
        final JTextField tf4 = new JTextField("Input Student's address here", 25);
        JButton submit = new JButton("Submit");
        JButton back = new JButton("Back");
        JPanel panel = new JPanel();
        panel.add(tf1);
        panel.add(tf2);
        panel.add(tf3);
        panel.add(tf4);
        panel.add(submit);
        panel.add(back);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
        submit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) 
                    {
                        String name = tf1.getText();
                        String ID = tf2.getText();
                        String email = tf3.getText();
                        String address = tf4.getText();
                        String fileName = ID + "Details.csv";
                        File file = new File(fileName);
                        FileWriter fw = new FileWriter(fileName); 
                        name = name.concat(",");
                        ID = ID.concat(",");
                        email = email.concat(",");
                        address = address.concat(",");
                        fw.write(name);
                        fw.write(ID);
                        fw.write(email);
                        fw.write(address);
                        fw.close();
                    }
                });


    back.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    frame.dispose();
                    GUI.AdminGUI();
                }
            });
        }
        catch(Exception e)
            {
                System.exit(0);
            }
}

有人能看出为什么会这样吗?我不明白为什么抛出异常

2 个答案:

答案 0 :(得分:0)

每当您在编写Java代码时遇到错误,当您尝试编译运行代码时是否会发生错误,请始终注意错误发生的位置(例如,行号,这种情况发生在哪种方法中)。并与您寻求帮助的人分享。

我最好的猜测是,java正在抱怨,因为您的addStudent方法会抛出Exception,而您的代码中使用addStudent的其他部分(可能是您的main方法)也不会处理抛出的可能Exception

答案 1 :(得分:0)

尝试将try/catch块放在actioPerformed()内。我认为你的捕获可能不在actionPerformed()

的范围内
submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) 
    {
        try {
             String name = tf1.getText();
             String ID = tf2.getText();
             String email = tf3.getText();
             String address = tf4.getText();
             String fileName = ID + "Details.csv";
             File file = new File(fileName);
             FileWriter fw = new FileWriter(fileName); 
             name = name.concat(",");
             ID = ID.concat(",");
             email = email.concat(",");
             address = address.concat(",");
             fw.write(name);
             fw.write(ID);
             fw.write(email);
             fw.write(address);
             fw.close();
        }
        catch (IOException ex){
             ex .printStackTrace();
             System.exit(0);
        }          
    }
});

如果你这样做,你不需要addStudent()抛出任何异常而你不需要它try/catch阻止。